BAEL-1107: update properties of Author (#2666)

* 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

* BAEL-1107: update formating and version of libs

* BAEL-1107: update properties of Author
This commit is contained in:
Dassi orleando 2017-09-24 18:32:50 +01:00 committed by maibin
parent c9072e54d1
commit 52ac2ba2d3
3 changed files with 22 additions and 41 deletions

View File

@ -19,22 +19,14 @@ public abstract class _Author extends CayenneDataObject {
public static final String ID_PK_COLUMN = "id";
public static final Property<String> FIRSTNAME = Property.create("firstname", String.class);
public static final Property<String> LASTNAME = Property.create("lastname", String.class);
public static final Property<String> NAME = Property.create("name", String.class);
public static final Property<List<Article>> ARTICLES = Property.create("articles", List.class);
public void setFirstname(String firstname) {
writeProperty("firstname", firstname);
public void setName(String name) {
writeProperty("name", name);
}
public String getFirstname() {
return (String)readProperty("firstname");
}
public void setLastname(String lastname) {
writeProperty("lastname", lastname);
}
public String getLastname() {
return (String)readProperty("lastname");
public String getName() {
return (String)readProperty("name");
}
public void addToArticles(Article obj) {

View File

@ -11,17 +11,15 @@
<db-attribute name="title" type="VARCHAR" isMandatory="true" length="254"/>
</db-entity>
<db-entity name="author" catalog="intro_cayenne">
<db-attribute name="firstname" type="VARCHAR" isMandatory="true" length="254"/>
<db-attribute name="id" type="INTEGER" isPrimaryKey="true" isGenerated="true" isMandatory="true" length="10"/>
<db-attribute name="lastname" type="VARCHAR" isMandatory="true" length="254"/>
<db-attribute name="name" type="VARCHAR" isMandatory="true" length="254"/>
</db-entity>
<obj-entity name="Article" className="com.baeldung.apachecayenne.persistent.Article" dbEntityName="article">
<obj-attribute name="content" type="java.lang.String" db-attribute-path="content"/>
<obj-attribute name="title" type="java.lang.String" db-attribute-path="title"/>
</obj-entity>
<obj-entity name="Author" className="com.baeldung.apachecayenne.persistent.Author" dbEntityName="author">
<obj-attribute name="firstname" type="java.lang.String" db-attribute-path="firstname"/>
<obj-attribute name="lastname" type="java.lang.String" db-attribute-path="lastname"/>
<obj-attribute name="name" type="java.lang.String" db-attribute-path="name"/>
</obj-entity>
<db-relationship name="author" source="article" target="author" toMany="false">
<db-attribute-pair source="author_id" target="id"/>

View File

@ -39,8 +39,7 @@ public class CayenneOperationTests {
@Test
public void givenAuthor_whenInsert_thenWeGetOneRecordInTheDatabase() {
Author author = context.newObject(Author.class);
author.setFirstname("Paul");
author.setLastname("Smith");
author.setName("Paul");
context.commitChanges();
@ -51,28 +50,24 @@ public class CayenneOperationTests {
@Test
public void givenAuthor_whenInsert_andQueryByFirstName_thenWeGetTheAuthor() {
Author author = context.newObject(Author.class);
author.setFirstname("Paul");
author.setLastname("Smith");
author.setName("Paul");
context.commitChanges();
Author expectedAuthor = ObjectSelect.query(Author.class)
.where(Author.FIRSTNAME.eq("Paul"))
.where(Author.NAME.eq("Paul"))
.selectOne(context);
assertEquals("Paul", expectedAuthor.getFirstname());
assertEquals("Smith", expectedAuthor.getLastname());
assertEquals("Paul", expectedAuthor.getName());
}
@Test
public void givenTwoAuthor_whenInsert_andQueryAll_thenWeGetTwoAuthors() {
Author firstAuthor = context.newObject(Author.class);
firstAuthor.setFirstname("Paul");
firstAuthor.setLastname("Smith");
firstAuthor.setName("Paul");
Author secondAuthor = context.newObject(Author.class);
secondAuthor.setFirstname("Ludovic");
secondAuthor.setLastname("Garcia");
secondAuthor.setName("Ludovic");
context.commitChanges();
@ -83,44 +78,40 @@ public class CayenneOperationTests {
@Test
public void givenAuthor_whenUpdating_thenWeGetAnUpatedeAuthor() {
Author author = context.newObject(Author.class);
author.setFirstname("Paul");
author.setLastname("Smith");
author.setName("Paul");
context.commitChanges();
Author expectedAuthor = ObjectSelect.query(Author.class)
.where(Author.FIRSTNAME.eq("Paul"))
.where(Author.NAME.eq("Paul"))
.selectOne(context);
expectedAuthor.setLastname("Smith 2");
expectedAuthor.setName("Garcia");
context.commitChanges();
assertEquals(author.getFirstname(), expectedAuthor.getFirstname());
assertEquals(author.getLastname(), expectedAuthor.getLastname());
assertEquals(author.getName(), expectedAuthor.getName());
}
@Test
public void givenAuthor_whenDeleting_thenWeLostHisDetails() {
Author author = context.newObject(Author.class);
author.setFirstname("Paul");
author.setLastname("Smith");
author.setName("Paul");
context.commitChanges();
Author savedAuthor = ObjectSelect.query(Author.class)
.where(Author.FIRSTNAME.eq("Paul")).selectOne(context);
.where(Author.NAME.eq("Paul")).selectOne(context);
if(savedAuthor != null) {
context.deleteObjects(author);
context.commitChanges();
}
Author expectedAuthor = ObjectSelect.query(Author.class)
.where(Author.FIRSTNAME.eq("Paul")).selectOne(context);
.where(Author.NAME.eq("Paul")).selectOne(context);
assertNull(expectedAuthor);
}
@Test
public void givenAuthor_whenAttachingToArticle_thenTheRelationIsMade() {
Author author = context.newObject(Author.class);
author.setFirstname("Paul");
author.setLastname("Smith");
author.setName("Paul");
Article article = context.newObject(Article.class);
article.setTitle("My post title");
@ -130,7 +121,7 @@ public class CayenneOperationTests {
context.commitChanges();
Author expectedAuthor = ObjectSelect.query(Author.class)
.where(Author.LASTNAME.eq("Smith"))
.where(Author.NAME.eq("Paul"))
.selectOne(context);
Article expectedArticle = (expectedAuthor.getArticles()).get(0);