Skip to content

Commit 16b6e08

Browse files
Dassi orleandomaibin
authored andcommitted
BAEL-1107 : Introduction to Apache Cayenne Orm (eugenp#2650)
* Different types of bean injection in Spring * Difference between two dates in java * Update README.md * Simple clean of difference between dates * Clean my test article * Improve dates diff: for dates and datetimes * Move difference between dates from core-java to libraries * BAEL-890 - Kotlin-Allopen with Spring example * BAEL-1107 - Introduction to Apache Cayenne Orm
1 parent 1852438 commit 16b6e08

10 files changed

Lines changed: 369 additions & 0 deletions

File tree

‎apache-cayenne/pom.xml‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<artifactId>apache-cayenne</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>apache-cayenne</name>
11+
<description>Introduction to Apache Cayenne</description>
12+
13+
<parent>
14+
<groupId>com.baeldung</groupId>
15+
<artifactId>parent-modules</artifactId>
16+
<version>1.0.0-SNAPSHOT</version>
17+
</parent>
18+
19+
<properties>
20+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
22+
<java.version>1.8</java.version>
23+
<mysql.connector.version>5.1.31</mysql.connector.version>
24+
<cayenne.version>4.0.M5</cayenne.version>
25+
<junit.version>4.12</junit.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.apache.cayenne</groupId>
31+
<artifactId>cayenne-server</artifactId>
32+
<version>${cayenne.version}</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>mysql</groupId>
36+
<artifactId>mysql-connector-java</artifactId>
37+
<version>${mysql.connector.version}</version>
38+
<scope>runtime</scope>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>junit</groupId>
43+
<artifactId>junit</artifactId>
44+
<version>${junit.version}</version>
45+
<scope>test</scope>
46+
</dependency>
47+
</dependencies>
48+
49+
<build>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.apache.cayenne.plugins</groupId>
53+
<artifactId>cayenne-modeler-maven-plugin</artifactId>
54+
<version>${cayenne.version}</version>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
59+
</project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.apachecayenne.persistent;
2+
3+
import com.baeldung.apachecayenne.persistent.auto._Article;
4+
5+
public class Article extends _Article {
6+
7+
private static final long serialVersionUID = 1L;
8+
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.apachecayenne.persistent;
2+
3+
import com.baeldung.apachecayenne.persistent.auto._Author;
4+
5+
public class Author extends _Author {
6+
7+
private static final long serialVersionUID = 1L;
8+
9+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.apachecayenne.persistent.auto;
2+
3+
import org.apache.cayenne.CayenneDataObject;
4+
import org.apache.cayenne.exp.Property;
5+
6+
import com.baeldung.apachecayenne.persistent.Author;
7+
8+
/**
9+
* Class _Article was generated by Cayenne.
10+
* It is probably a good idea to avoid changing this class manually,
11+
* since it may be overwritten next time code is regenerated.
12+
* If you need to make any customizations, please use subclass.
13+
*/
14+
public abstract class _Article extends CayenneDataObject {
15+
16+
private static final long serialVersionUID = 1L;
17+
18+
public static final String ID_PK_COLUMN = "id";
19+
20+
public static final Property<String> CONTENT = Property.create("content", String.class);
21+
public static final Property<String> TITLE = Property.create("title", String.class);
22+
public static final Property<Author> AUTHOR = Property.create("author", Author.class);
23+
24+
public void setContent(String content) {
25+
writeProperty("content", content);
26+
}
27+
public String getContent() {
28+
return (String)readProperty("content");
29+
}
30+
31+
public void setTitle(String title) {
32+
writeProperty("title", title);
33+
}
34+
public String getTitle() {
35+
return (String)readProperty("title");
36+
}
37+
38+
public void setAuthor(Author author) {
39+
setToOneTarget("author", author, true);
40+
}
41+
42+
public Author getAuthor() {
43+
return (Author)readProperty("author");
44+
}
45+
46+
47+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.apachecayenne.persistent.auto;
2+
3+
import java.util.List;
4+
5+
import org.apache.cayenne.CayenneDataObject;
6+
import org.apache.cayenne.exp.Property;
7+
8+
import com.baeldung.apachecayenne.persistent.Article;
9+
10+
/**
11+
* Class _Author was generated by Cayenne.
12+
* It is probably a good idea to avoid changing this class manually,
13+
* since it may be overwritten next time code is regenerated.
14+
* If you need to make any customizations, please use subclass.
15+
*/
16+
public abstract class _Author extends CayenneDataObject {
17+
18+
private static final long serialVersionUID = 1L;
19+
20+
public static final String ID_PK_COLUMN = "id";
21+
22+
public static final Property<String> FIRSTNAME = Property.create("firstname", String.class);
23+
public static final Property<String> LASTNAME = Property.create("lastname", String.class);
24+
public static final Property<List<Article>> ARTICLES = Property.create("articles", List.class);
25+
26+
public void setFirstname(String firstname) {
27+
writeProperty("firstname", firstname);
28+
}
29+
public String getFirstname() {
30+
return (String)readProperty("firstname");
31+
}
32+
33+
public void setLastname(String lastname) {
34+
writeProperty("lastname", lastname);
35+
}
36+
public String getLastname() {
37+
return (String)readProperty("lastname");
38+
}
39+
40+
public void addToArticles(Article obj) {
41+
addToManyTarget("articles", obj, true);
42+
}
43+
public void removeFromArticles(Article obj) {
44+
removeToManyTarget("articles", obj, true);
45+
}
46+
@SuppressWarnings("unchecked")
47+
public List<Article> getArticles() {
48+
return (List<Article>)readProperty("articles");
49+
}
50+
51+
52+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<domain project-version="9">
3+
<map name="datamap"/>
4+
5+
<node name="datanode"
6+
factory="org.apache.cayenne.configuration.server.XMLPoolingDataSourceFactory"
7+
schema-update-strategy="org.apache.cayenne.access.dbsync.CreateIfNoSchemaStrategy"
8+
>
9+
<map-ref name="datamap"/>
10+
<data-source>
11+
<driver value="com.mysql.jdbc.Driver"/>
12+
<url value="jdbc:mysql://localhost:3306/intro_cayenne"/>
13+
<connectionPool min="1" max="1"/>
14+
<login userName="root" password="root"/>
15+
</data-source>
16+
</node>
17+
</domain>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<data-map xmlns="http://cayenne.apache.org/schema/9/modelMap"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://cayenne.apache.org/schema/9/modelMap http://cayenne.apache.org/schema/9/modelMap.xsd"
5+
project-version="9">
6+
<property name="defaultPackage" value="com.baeldung.apachecayenne.persistent"/>
7+
<db-entity name="article" catalog="intro_cayenne">
8+
<db-attribute name="author_id" type="INTEGER" isMandatory="true" length="10"/>
9+
<db-attribute name="content" type="VARCHAR" isMandatory="true" length="254"/>
10+
<db-attribute name="id" type="INTEGER" isPrimaryKey="true" isGenerated="true" isMandatory="true" length="10"/>
11+
<db-attribute name="title" type="VARCHAR" isMandatory="true" length="254"/>
12+
</db-entity>
13+
<db-entity name="author" catalog="intro_cayenne">
14+
<db-attribute name="firstname" type="VARCHAR" isMandatory="true" length="254"/>
15+
<db-attribute name="id" type="INTEGER" isPrimaryKey="true" isGenerated="true" isMandatory="true" length="10"/>
16+
<db-attribute name="lastname" type="VARCHAR" isMandatory="true" length="254"/>
17+
</db-entity>
18+
<obj-entity name="Article" className="com.baeldung.apachecayenne.persistent.Article" dbEntityName="article">
19+
<obj-attribute name="content" type="java.lang.String" db-attribute-path="content"/>
20+
<obj-attribute name="title" type="java.lang.String" db-attribute-path="title"/>
21+
</obj-entity>
22+
<obj-entity name="Author" className="com.baeldung.apachecayenne.persistent.Author" dbEntityName="author">
23+
<obj-attribute name="firstname" type="java.lang.String" db-attribute-path="firstname"/>
24+
<obj-attribute name="lastname" type="java.lang.String" db-attribute-path="lastname"/>
25+
</obj-entity>
26+
<db-relationship name="author" source="article" target="author" toMany="false">
27+
<db-attribute-pair source="author_id" target="id"/>
28+
</db-relationship>
29+
<db-relationship name="articles" source="author" target="article" toMany="true">
30+
<db-attribute-pair source="id" target="author_id"/>
31+
</db-relationship>
32+
<obj-relationship name="author" source="Article" target="Author" deleteRule="Nullify" db-relationship-path="author"/>
33+
<obj-relationship name="articles" source="Author" target="Article" deleteRule="Deny" db-relationship-path="articles"/>
34+
</data-map>
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package com.baeldung.apachecayenne;
2+
3+
import com.baeldung.apachecayenne.persistent.Article;
4+
import com.baeldung.apachecayenne.persistent.Author;
5+
import org.apache.cayenne.ObjectContext;
6+
import org.apache.cayenne.configuration.server.ServerRuntime;
7+
import org.apache.cayenne.query.ObjectSelect;
8+
import org.apache.cayenne.query.SQLTemplate;
9+
import org.junit.After;
10+
import org.junit.BeforeClass;
11+
import org.junit.Test;
12+
13+
import java.util.List;
14+
15+
import static junit.framework.Assert.assertEquals;
16+
import static org.junit.Assert.assertNull;
17+
18+
19+
public class CayenneOperationTests {
20+
private static ObjectContext context = null;
21+
22+
@BeforeClass
23+
public static void setupTheCayenneContext(){
24+
ServerRuntime cayenneRuntime = ServerRuntime.builder()
25+
.addConfig("cayenne-project.xml")
26+
.build();
27+
context = cayenneRuntime.newContext();
28+
}
29+
30+
@After
31+
public void deleteAllRecords(){
32+
SQLTemplate deleteArticles = new SQLTemplate(Article.class, "delete from article");
33+
SQLTemplate deleteAuthors = new SQLTemplate(Author.class, "delete from author");
34+
35+
context.performGenericQuery(deleteArticles);
36+
context.performGenericQuery(deleteAuthors);
37+
}
38+
39+
@Test
40+
public void givenAuthor_whenInsert_thenWeGetOneRecordInTheDatabase(){
41+
Author author = context.newObject(Author.class);
42+
author.setFirstname("Paul");
43+
author.setLastname("Smith");
44+
45+
context.commitChanges();
46+
47+
long records = ObjectSelect.dataRowQuery(Author.class).selectCount(context);
48+
assertEquals(1, records);
49+
}
50+
51+
@Test
52+
public void givenAuthor_whenInsert_andQueryByFirstName_thenWeGetTheAuthor(){
53+
Author author = context.newObject(Author.class);
54+
author.setFirstname("Paul");
55+
author.setLastname("Smith");
56+
57+
context.commitChanges();
58+
59+
Author expectedAuthor = ObjectSelect.query(Author.class)
60+
.where(Author.FIRSTNAME.eq("Paul"))
61+
.selectOne(context);
62+
63+
assertEquals("Paul", expectedAuthor.getFirstname());
64+
assertEquals("Smith", expectedAuthor.getLastname());
65+
}
66+
67+
@Test
68+
public void givenTwoAuthor_whenInsert_andQueryAll_thenWeGetTwoAuthors(){
69+
Author firstAuthor = context.newObject(Author.class);
70+
firstAuthor.setFirstname("Paul");
71+
firstAuthor.setLastname("Smith");
72+
73+
Author secondAuthor = context.newObject(Author.class);
74+
secondAuthor.setFirstname("Ludovic");
75+
secondAuthor.setLastname("Garcia");
76+
77+
context.commitChanges();
78+
79+
List<Author> authors = ObjectSelect.query(Author.class).select(context);
80+
assertEquals(2, authors.size());
81+
}
82+
83+
@Test
84+
public void givenAuthor_whenUpdating_thenWeGetAnUpatedeAuthor(){
85+
Author author = context.newObject(Author.class);
86+
author.setFirstname("Paul");
87+
author.setLastname("Smith");
88+
context.commitChanges();
89+
90+
Author expectedAuthor = ObjectSelect.query(Author.class)
91+
.where(Author.FIRSTNAME.eq("Paul"))
92+
.selectOne(context);
93+
expectedAuthor.setLastname("Smith 2");
94+
context.commitChanges();
95+
96+
assertEquals(author.getFirstname(), expectedAuthor.getFirstname());
97+
assertEquals(author.getLastname(), expectedAuthor.getLastname());
98+
}
99+
100+
@Test
101+
public void givenAuthor_whenDeleting_thenWeLostHisDetails(){
102+
Author author = context.newObject(Author.class);
103+
author.setFirstname("Paul");
104+
author.setLastname("Smith");
105+
context.commitChanges();
106+
107+
Author savedAuthor = ObjectSelect.query(Author.class)
108+
.where(Author.FIRSTNAME.eq("Paul")).selectOne(context);
109+
if(savedAuthor != null) {
110+
context.deleteObjects(author);
111+
context.commitChanges();
112+
}
113+
114+
Author expectedAuthor = ObjectSelect.query(Author.class)
115+
.where(Author.FIRSTNAME.eq("Paul")).selectOne(context);
116+
assertNull(expectedAuthor);
117+
}
118+
119+
@Test
120+
public void givenAuthor_whenAttachingToArticle_thenTheRelationIsMade(){
121+
Author author = context.newObject(Author.class);
122+
author.setFirstname("Paul");
123+
author.setLastname("Smith");
124+
125+
Article article = context.newObject(Article.class);
126+
article.setTitle("My post title");
127+
article.setContent("The content");
128+
article.setAuthor(author);
129+
130+
context.commitChanges();
131+
132+
Author expectedAuthor = ObjectSelect.query(Author.class)
133+
.where(Author.LASTNAME.eq("Smith"))
134+
.selectOne(context);
135+
136+
Article expectedArticle = (expectedAuthor.getArticles()).get(0);
137+
assertEquals(article.getTitle(), expectedArticle.getTitle());
138+
}
139+
140+
}

‎libraries/pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@
620620
<eclipse-collections.version>8.2.0</eclipse-collections.version>
621621
<streamex.version>0.6.5</streamex.version>
622622
<vavr.version>0.9.0</vavr.version>
623+
<geotools.version>15.2</geotools.version>
623624
<joda-time.version>2.9.9</joda-time.version>
624625
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
625626
<retrofit.version>2.3.0</retrofit.version>

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
</properties>
2929

3030
<modules>
31+
<module>apache-cayenne</module>
3132
<module>aws</module>
3233
<module>akka-streams</module>
3334
<module>algorithms</module>

0 commit comments

Comments
 (0)