At line 4 changed 1 line. |
;:''For further details about the specifics regarding creating entities, XDoclet tags for Hibernate, DAO development, etc., please refer to [Creating new DAOs and Objects in AppFuse|CreateDAO].'' |
;:''For further details about the specifics regarding creating objects, XDoclet tags for Hibernate, DAO development, etc., please refer to [Creating new DAOs and Objects in AppFuse|CreateDAO].'' |
At line 6 removed 2 lines. |
<button type="button" onclick="toggleCode()">Show/Hide Code</button> |
|
At line 17 removed 2 lines. |
For our purposes, the Weblog entity is a list of available weblogs. A Weblog object has the following properties an id, a username (for the user who created the blog), a dateCreated, and a blogTitle. Every Entry object has an id, a text, and a timeCreated property. |
The first thing to do is create some objects to persist. Let's create both a "Weblog" object and an Entry object (in the src/dao/**/model directory). Basic XDoclet tags for these entities are show as well. |
At line 20 changed 1 line. |
[{Java2HtmlPlugin |
In this example, the ''Weblog'' object is used to indentify a person's blog. This class has the following properties: |
At line 22 changed 1 line. |
package org.appfuse.model; |
* weblogId |
* username |
* blogTitle |
* dateCreated |
At line 24 changed 1 line. |
import java.util.Date; |
The ''Entry'' object is used to contain a listing of a person's blog entries in their Weblog. This class contains the following properties: |
At line 26 changed 4 lines. |
import org.apache.commons.lang.builder.EqualsBuilder; |
import org.apache.commons.lang.builder.HashCodeBuilder; |
import org.apache.commons.lang.builder.ToStringBuilder; |
import org.apache.commons.lang.builder.ToStringStyle; |
* entryId |
* text |
* timeCreated |
At line 31 changed 4 lines. |
/** |
* @hibernate.class table="weblog" |
*/ |
public class Weblog extends BaseObject { |
%%note __NOTE:__ The primary keys are prefixed with their entity name to avoid confusion. I generally recommend using "id" for your entities, but wanted to make thing clearer in this tutorial.%% |
At line 36 changed 5 lines. |
private Long weblogId; |
private String username; |
private Date dateCreated; |
private String blogTitle; |
private Integer version; |
The first thing you need to do in this tutorial is these two object to persist. Create both a ''Weblog.java'' class and an ''Entry.java'' class (in the src/dao/**/model directory). The necessary XDoclet tags for these entities is included on the ''getter'' method's javadoc. |
At line 42 changed 8 lines. |
/** |
* @return Returns the blogTitle. |
* |
* @hibernate.property column="blog_title" |
*/ |
public String getBlogTitle() { |
return blogTitle; |
} |
* [View Weblog.java|Weblog.java] |
* [View Entry.java|Entry.java] |
At line 51 changed 7 lines. |
/** |
* @param blogTitle |
* The blogTitle to set. |
*/ |
public void setBlogTitle(String blogTitle) { |
this.blogTitle = blogTitle; |
} |
;:''Rather than fill up this tutorial with large blocks of Java code, the necessary files are attached and linked to. Small code snippets are used where appropriate.'' |
At line 59 changed 8 lines. |
/** |
* @return Returns the dateCreated. |
* |
* @hibernate.property column="date_created" |
*/ |
public Date getDateCreated() { |
return dateCreated; |
} |
%%note __Tip:__ The equals(), hashCode() and toString() methods can be generated by your IDE. If you're using Eclipse, get the [Commonclipse|http://commonclipse.sourceforge.net/] plugin. For IDEA, the first two are built into the "Generate..." dialog, and toString() is a plugin you can download.%% |
At line 68 removed 233 lines. |
/** |
* @param dateCreated |
* The dateCreated to set. |
*/ |
public void setDateCreated(Date dateCreated) { |
this.dateCreated = dateCreated; |
} |
|
/** |
* @return Returns the id. |
* |
* @hibernate.id column="weblog_id" generator-class="native" |
* unsaved-value="null" |
*/ |
public Long getWeblogId() { |
return weblogId; |
} |
|
/** |
* @param id |
* The id to set. |
*/ |
public void setWeblogId(Long weblogId) { |
this.weblogId = weblogId; |
} |
|
/** |
* @return Returns the username. |
* |
* @hibernate.property column="username" |
*/ |
public String getUsername() { |
return username; |
} |
|
/** |
* @param username |
* The username to set. |
*/ |
public void setUsername(String username) { |
this.username = username; |
} |
|
/** |
* @return Returns the version. |
* |
* @hibernate.version |
*/ |
public Integer getVersion() { |
return version; |
} |
|
/** |
* @param version |
* The version to set. |
*/ |
public void setVersion(Integer version) { |
this.version = version; |
} |
|
/** |
* @see java.lang.Object#equals(Object) |
*/ |
public boolean equals(Object object) { |
if (!(object instanceof Weblog)) { |
return false; |
} |
Weblog rhs = (Weblog) object; |
return new EqualsBuilder().append(this.blogTitle, rhs.blogTitle) |
.append(this.username, rhs.username).append(this.dateCreated, |
rhs.dateCreated).append(this.weblogId, rhs.weblogId).append( |
this.version, rhs.version).isEquals(); |
} |
|
/** |
* @see java.lang.Object#hashCode() |
*/ |
public int hashCode() { |
return new HashCodeBuilder(2066507029, -390167195).append( |
this.blogTitle).append(this.username).append(this.dateCreated) |
.append(this.weblogId).append(this.version).toHashCode(); |
} |
|
/** |
* @see java.lang.Object#toString() |
*/ |
public String toString() { |
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) |
.append("id", this.weblogId).append("version", this.version).append( |
"username", this.username).append("blogTitle", |
this.blogTitle).append("dateCreated", this.dateCreated) |
.toString(); |
} |
} |
|
}] |
|
[{Java2HtmlPlugin |
|
package org.appfuse.model; |
|
import java.util.Date; |
|
import org.apache.commons.lang.builder.EqualsBuilder; |
import org.apache.commons.lang.builder.HashCodeBuilder; |
import org.apache.commons.lang.builder.ToStringBuilder; |
import org.apache.commons.lang.builder.ToStringStyle; |
|
/** |
* @hibernate.class table="entry" |
*/ |
|
|
|
public class Entry extends BaseObject { |
|
private Long id; |
private String text; |
private Date timeCreated; |
private Integer version; |
|
/* |
* Generate your getters and setters using your favorite IDE: In Eclipse: |
* Right-click -> Source -> Generate Getters and Setters |
*/ |
|
/** |
* @return Returns the id. |
* |
* @hibernate.id column="entry_id" generator-class="native" |
* unsaved-value="null" |
*/ |
public Long getEntryId() { |
return entryId; |
} |
|
/** |
* @param id |
* The id to set. |
*/ |
public void setEntryId(Long entryId) { |
this.entryId = entryId; |
} |
|
/** |
* @return Returns the text. |
* |
* @hibernate.property column="entry_text" |
*/ |
public String getText() { |
return text; |
} |
|
/** |
* @param text |
* The text to set. |
*/ |
public void setText(String text) { |
this.text = text; |
} |
|
/** |
* @return Returns the timeCreated. |
* |
* @hibernate.property column="time_created" |
*/ |
public Date getTimeCreated() { |
return timeCreated; |
} |
|
/** |
* @param timeCreated |
* The timeCreated to set. |
*/ |
public void setTimeCreated(Date timeCreated) { |
this.timeCreated = timeCreated; |
} |
|
/** |
* @return Returns the version. |
* |
* @hibernate.version |
*/ |
public Integer getVersion() { |
|
return version; |
} |
|
/** |
* @param version |
* The version to set. |
*/ |
public void setVersion(Integer version) { |
this.version = version; |
} |
|
/** |
* @see java.lang.Object#equals(Object) |
*/ |
public boolean equals(Object object) { |
if (!(object instanceof Entry)) { |
return false; |
} |
Entry rhs = (Entry) object; |
return new EqualsBuilder().append(this.text, rhs.text).append( |
this.timeCreated, rhs.timeCreated).append(this.id, rhs.id) |
.append(this.version, rhs.version).isEquals(); |
} |
|
/** |
* @see java.lang.Object#hashCode() |
*/ |
public int hashCode() { |
return new HashCodeBuilder(-880342185, -1668369001).append(this.text) |
.append(this.timeCreated).append(this.id).append(this.version) |
.toHashCode(); |
} |
|
/** |
* @see java.lang.Object#toString() |
*/ |
public String toString() { |
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) |
.append("text", this.text).append("id", this.id).append( |
"version", this.version).append("timeCreated", |
this.timeCreated).toString(); |
} |
} |
}] |
|
|
|
|
At line 305 changed 1 line. |
[{Java2HtmlPlugin |
* [View Category.java|Category.java] |
At line 307 changed 1 line. |
package org.appfuse.model; |
The many-to-one relationship between entry and category can be established using XDoclet tags. Hibernate relationships can typically be established on either side of the relationship or as a bi-directional one as well. For our purposes,this relationship will be maintained by an extra property and collection held by Entry. The list of possible categories for a weblog entry will eventually be represented as a drop-down on the UI. |
At line 309 removed 126 lines. |
import org.apache.commons.lang.builder.EqualsBuilder; |
import org.apache.commons.lang.builder.HashCodeBuilder; |
import org.apache.commons.lang.builder.ToStringBuilder; |
import org.apache.commons.lang.builder.ToStringStyle; |
|
/** |
* @hibernate.class table="category" |
*/ |
public class Category extends BaseObject { |
|
private Long id; |
private String categoryName; |
private String categoryDescription; |
private Integer version; |
|
/** |
* @return Returns the categoryDescription. |
* |
* @hibernate.property column="category_description" |
*/ |
public String getCategoryDescription() { |
return categoryDescription; |
} |
|
/** |
* @param categoryDescription |
* The categoryDescription to set. |
*/ |
public void setCategoryDescription(String categoryDescription) { |
this.categoryDescription = categoryDescription; |
} |
|
/** |
* @return Returns the categoryName. |
* |
* @hibernate.property column="category_name" |
*/ |
public String getCategoryName() { |
return categoryName; |
} |
|
/** |
* @param categoryName |
* The categoryName to set. |
*/ |
public void setCategoryName(String categoryName) { |
this.categoryName = categoryName; |
|
|
} |
|
/** |
* @return Returns the id. |
* |
|
* @hibernate.id column="category_id" generator-class="native" |
* unsaved-value="null" |
*/ |
public Long getId() { |
return id; |
} |
|
/** |
* @param id |
* The id to set. |
*/ |
public void setId(Long id) { |
this.id = id; |
} |
|
/** |
* @return Returns the version. |
* |
* @hibernate.version |
*/ |
public Integer getVersion() { |
return version; |
} |
|
/** |
* @param version |
* The version to set. |
*/ |
public void setVersion(Integer version) { |
this.version = version; |
} |
|
/** |
* @see java.lang.Object#equals(Object) |
*/ |
public boolean equals(Object object) { |
if (!(object instanceof Category)) { |
return false; |
} |
Category rhs = (Category) object; |
return new EqualsBuilder().append(this.categoryDescription, |
rhs.categoryDescription).append(this.categoryName, |
rhs.categoryName).append(this.id, rhs.id).append(this.version, |
rhs.version).isEquals(); |
} |
|
/** |
* @see java.lang.Object#hashCode() |
*/ |
public int hashCode() { |
return new HashCodeBuilder(-837614407, -1874103513).append( |
this.categoryDescription).append(this.categoryName).append( |
this.id).append(this.version).toHashCode(); |
} |
|
/** |
* @see java.lang.Object#toString() |
*/ |
public String toString() { |
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) |
.append("id", this.id) |
.append("categoryName", this.categoryName).append("version", |
this.version).append("categoryDescription", |
this.categoryDescription).toString(); |
} |
} |
|
}] |
|
The many-to-one relationship between entry and category can be established by the entity classes. Hibernate relationships can typically be established on either side of the relationship or as a bi-directional one as well. For our purposes,this relationship will be maintained by an extra property and collection held by Entry. |
|
At line 437 changed 1 line. |
package org.appfuse.model; |
|
private Long categoryId; |
private Category category; |
At line 439 changed 1 line. |
import java.util.Date; |
/** |
* @hibernate.many-to-one insert="false" update="false" cascade="none" |
* column="category_id" outer-join="true" |
*/ |
public Category getCategory() { |
return category; |
} |
At line 441 changed 4 lines. |
import org.apache.commons.lang.builder.EqualsBuilder; |
import org.apache.commons.lang.builder.HashCodeBuilder; |
import org.apache.commons.lang.builder.ToStringBuilder; |
import org.apache.commons.lang.builder.ToStringStyle; |
public void setCategory(Category category) { |
this.category = category; |
} |
At line 446 changed 4 lines. |
/** |
* @hibernate.class table="entry" |
*/ |
public class Entry extends BaseObject { |
/** |
* @hibernate.property column="category_id" |
*/ |
public Long getCategoryId() { |
return categoryId; |
} |
At line 451 changed 6 lines. |
private Long entryId; |
private String text; |
private Date timeCreated; |
private Integer version; |
private Long categoryId; |
private Category category; |
public void setCategoryId(Long categoryId) { |
this.categoryId = categoryId; |
} |
At line 458 removed 150 lines. |
/* |
* Generate your getters and setters using your favorite IDE: In Eclipse: |
* Right-click -> Source -> Generate Getters and Setters |
*/ |
|
/** |
* @return Returns the category. |
* |
* @hibernate.many-to-one insert="false" update="false" cascade="none" |
* column="category_id" outer-join="true" |
*/ |
public Category getCategory() { |
return category; |
} |
|
|
|
/** |
* @param category |
* The category to set. |
*/ |
public void setCategory(Category category) { |
this.category = category; |
} |
|
/** |
* @return Returns the categoryId. |
* |
* @hibernate.property column="category_id" |
*/ |
public Long getCategoryId() { |
return categoryId; |
} |
|
/** |
* @param categoryId |
* The categoryId to set. |
* |
*/ |
public void setCategoryId(Long categoryId) { |
this.categoryId = categoryId; |
} |
|
/** |
* @return Returns the id. |
* |
* @hibernate.id column="entry_id" generator-class="native" |
* unsaved-value="null" |
*/ |
public Long getEntryId() { |
return entryId; |
} |
|
/** |
* @param id |
* The id to set. |
*/ |
public void setEntryId(Long entryId) { |
this.entryId = entryId; |
} |
|
/** |
* @return Returns the text. |
* |
* @hibernate.property column="entry_text" |
*/ |
public String getText() { |
return text; |
} |
|
/** |
* @param text |
* The text to set. |
*/ |
public void setText(String text) { |
this.text = text; |
} |
|
/** |
* @return Returns the timeCreated. |
* |
|
* @hibernate.property column="time_created" |
*/ |
public Date getTimeCreated() { |
return timeCreated; |
} |
|
/** |
* @param timeCreated |
* The timeCreated to set. |
*/ |
public void setTimeCreated(Date timeCreated) { |
this.timeCreated = timeCreated; |
} |
|
/** |
* @return Returns the version. |
* |
* @hibernate.version |
*/ |
public Integer getVersion() { |
return version; |
} |
|
/** |
* @param version |
* The version to set. |
*/ |
public void setVersion(Integer version) { |
this.version = version; |
} |
|
/** |
* @see java.lang.Object#equals(Object) |
*/ |
public boolean equals(Object object) { |
if (!(object instanceof Entry)) { |
return false; |
} |
Entry rhs = (Entry) object; |
return new EqualsBuilder().append(this.text, rhs.text).append( |
this.timeCreated, rhs.timeCreated).append(this.category, |
rhs.category).append(this.entryId, rhs.entryId).append( |
this.categoryId, rhs.categoryId).append(this.version, |
rhs.version).isEquals(); |
} |
|
/** |
* @see java.lang.Object#hashCode() |
*/ |
public int hashCode() { |
return new HashCodeBuilder(835056587, 1192670387).append(this.text) |
.append(this.timeCreated).append(this.category).append( |
this.entryId).append(this.categoryId).append( |
this.version).toHashCode(); |
} |
|
/** |
* @see java.lang.Object#toString() |
*/ |
public String toString() { |
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) |
.append("text", this.text).append("entryId", this.entryId) |
.append("version", this.version).append("categoryId", |
this.categoryId).append("category", this.category) |
.append("timeCreated", this.timeCreated).toString(); |
} |
} |
|
At line 612 changed 1 line. |
Modify the Weblog object and Entry object to represent the multiplicity of a weblog that can have many entries. This relationship is set on the Weblog class using a list. Hibernate tags are used to establish this relationship using a bag as the Hibernate collection type. |
Modify the Weblog object and Entry object to represent the multiplicity of a weblog that can have many entries. This relationship is set on the Weblog class using a list. XDoclet tags are used to establish this relationship using a __bag__ as the Hibernate collection type. |
At line 616 changed 1 line. |
package org.appfuse.model; |
private List entries; |
At line 618 changed 1 line. |
import java.util.Date; |
/** |
* @return Returns the entries. |
* |
* @hibernate.bag name="entries" lazy="true" inverse="true" cascade="delete" |
* @hibernate.collection-key column="entry_id" |
* @hibernate.collection-one-to-many class="org.appfuse.model.Entry" |
*/ |
public List getEntries() { |
return entries; |
} |
At line 620 changed 165 lines. |
import java.util.List; |
|
import org.apache.commons.lang.builder.EqualsBuilder; |
import org.apache.commons.lang.builder.HashCodeBuilder; |
import org.apache.commons.lang.builder.ToStringBuilder; |
import org.apache.commons.lang.builder.ToStringStyle; |
|
/** |
* @hibernate.class table="weblog" |
*/ |
public class Weblog extends BaseObject { |
|
private Long weblogId; |
private String username; |
private Date dateCreated; |
private String blogTitle; |
private Integer version; |
private List entries; |
|
/** |
* @return Returns the blogTitle. |
* |
* @hibernate.property column="blog_title" |
*/ |
public String getBlogTitle() { |
return blogTitle; |
} |
|
/** |
* @param blogTitle |
* The blogTitle to set. |
*/ |
public void setBlogTitle(String blogTitle) { |
this.blogTitle = blogTitle; |
} |
|
/** |
* @return Returns the dateCreated. |
* |
* @hibernate.property column="date_created" |
*/ |
public Date getDateCreated() { |
return dateCreated; |
} |
|
/** |
* @param dateCreated |
* The dateCreated to set. |
*/ |
public void setDateCreated(Date dateCreated) { |
this.dateCreated = dateCreated; |
} |
|
/** |
* @return Returns the entries. |
* |
* @hibernate.bag name="entries" lazy="true" inverse="true" |
* cascade="delete" |
* @hibernate.collection-key column="entry_id" |
* @hibernate.collection-one-to-many |
* class="org.appfuse.model.Entry" |
*/ |
public List getEntries() { |
return entries; |
} |
|
/** |
* @param entries |
* The entries to set. |
*/ |
public void setEntries(List entries) { |
this.entries = entries; |
} |
|
/** |
* @return Returns the id. |
* |
* @hibernate.id column="weblog_id" generator-class="native" |
* unsaved-value="null" |
*/ |
public Long getWeblogId() { |
return weblogId; |
} |
|
/** |
* @param id |
* The id to set. |
*/ |
public void setWeblogId(Long weblogId) { |
this.weblogId = weblogId; |
} |
|
/** |
* @return Returns the username. |
* |
* @hibernate.property column="username" |
*/ |
public String getUsername() { |
return username; |
} |
|
/** |
* @param username |
* The username to set. |
*/ |
public void setUsername(String username) { |
this.username = username; |
} |
|
/** |
* @return Returns the version. |
* |
* @hibernate.version |
*/ |
public Integer getVersion() { |
return version; |
} |
|
/** |
* @param version |
* The version to set. |
*/ |
public void setVersion(Integer version) { |
this.version = version; |
} |
|
/** |
* @see java.lang.Object#equals(Object) |
*/ |
public boolean equals(Object object) { |
if (!(object instanceof Weblog)) { |
return false; |
} |
Weblog rhs = (Weblog) object; |
return new EqualsBuilder().append(this.blogTitle, rhs.blogTitle) |
.append(this.weblogId, rhs.weblogId).append(this.username, |
rhs.username).append(this.entries, rhs.entries).append( |
this.dateCreated, rhs.dateCreated).append(this.version, |
rhs.version).isEquals(); |
} |
|
/** |
* @see java.lang.Object#hashCode() |
*/ |
public int hashCode() { |
return new HashCodeBuilder(-1412256665, -696169811).append( |
this.blogTitle).append(this.weblogId).append(this.username) |
.append(this.entries).append(this.dateCreated).append( |
this.version).toHashCode(); |
} |
|
/** |
* @see java.lang.Object#toString() |
*/ |
public String toString() { |
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) |
.append("weblogId", this.weblogId).append("version", |
this.version).append("username", this.username).append( |
"blogTitle", this.blogTitle).append("entries", |
this.entries).append("dateCreated", this.dateCreated) |
.toString(); |
} |
} |
|
|
public void setEntries(List entries) { |
this.entries = entries; |
} |
At line 791 changed 1 line. |
package org.appfuse.model; |
private Long weblogId; |
At line 793 changed 1 line. |
import java.util.Date; |
/** |
* @hibernate.property column="weblog_id" |
*/ |
public Long getWeblogId() { |
return weblogId; |
} |
At line 795 changed 185 lines. |
import org.apache.commons.lang.builder.EqualsBuilder; |
import org.apache.commons.lang.builder.HashCodeBuilder; |
import org.apache.commons.lang.builder.ToStringBuilder; |
import org.apache.commons.lang.builder.ToStringStyle; |
|
/** |
* @hibernate.class table="entry" |
*/ |
public class Entry extends BaseObject { |
|
private Long entryId; |
private String text; |
private Date timeCreated; |
private Integer version; |
private Category category; |
private Long categoryId; |
|
|
private Long weblogId; |
|
/* |
* Generate your getters and setters using your favorite IDE: In Eclipse: |
* Right-click -> Source -> Generate Getters and Setters |
*/ |
|
/** |
* @return Returns the category. |
* |
* @hibernate.many-to-one insert="false" update="false" cascade="none" |
* column="category_id" outer-join="true" |
*/ |
public Category getCategory() { |
return category; |
} |
|
/** |
* @param category |
* The category to set. |
*/ |
public void setCategory(Category category) { |
this.category = category; |
} |
|
/** |
* @return Returns the categoryId. |
* |
* @hibernate.property column="category_id" |
*/ |
public Long getCategoryId() { |
return categoryId; |
} |
|
/** |
* @param categoryId |
* The categoryId to set. |
* |
*/ |
public void setCategoryId(Long categoryId) { |
this.categoryId = categoryId; |
} |
|
/** |
* @return Returns the id. |
* |
* @hibernate.id column="entry_id" generator-class="native" |
* unsaved-value="null" |
*/ |
public Long getEntryId() { |
return entryId; |
} |
|
/** |
* @param id |
* The id to set. |
*/ |
public void setEntryId(Long entryId) { |
this.entryId = entryId; |
} |
|
/** |
* @return Returns the text. |
* |
* @hibernate.property column="entry_text" |
*/ |
public String getText() { |
return text; |
} |
|
/** |
* @param text |
* The text to set. |
*/ |
public void setText(String text) { |
this.text = text; |
} |
|
/** |
* @return Returns the timeCreated. |
* |
* @hibernate.property column="time_created" |
*/ |
public Date getTimeCreated() { |
return timeCreated; |
} |
|
/** |
* @param timeCreated |
* The timeCreated to set. |
*/ |
public void setTimeCreated(Date timeCreated) { |
this.timeCreated = timeCreated; |
} |
|
/** |
* @return Returns the version. |
* |
* @hibernate.version |
*/ |
public Integer getVersion() { |
return version; |
} |
|
/** |
* @param version |
* The version to set. |
*/ |
public void setVersion(Integer version) { |
this.version = version; |
} |
|
/** |
* @return Returns the weblogId. |
* |
* @hibernate.property column="weblog_id" |
*/ |
public Long getWeblogId() { |
return weblogId; |
} |
|
/** |
* @param weblogId |
* The weblogId to set. |
*/ |
public void setWeblogId(Long weblogId) { |
this.weblogId = weblogId; |
} |
|
/** |
* @see java.lang.Object#equals(Object) |
*/ |
public boolean equals(Object object) { |
if (!(object instanceof Entry)) { |
return false; |
} |
Entry rhs = (Entry) object; |
return new EqualsBuilder().append(this.text, rhs.text).append( |
this.timeCreated, rhs.timeCreated).append(this.weblogId, |
rhs.weblogId).append(this.category, rhs.category).append( |
this.entryId, rhs.entryId).append(this.categoryId, |
rhs.categoryId).append(this.version, rhs.version).isEquals(); |
} |
|
/** |
* @see java.lang.Object#hashCode() |
*/ |
public int hashCode() { |
return new HashCodeBuilder(-1879789101, 535224049).append(this.text) |
.append(this.timeCreated).append(this.weblogId).append( |
this.category).append(this.entryId).append( |
this.categoryId).append(this.version).toHashCode(); |
} |
|
/** |
* @see java.lang.Object#toString() |
*/ |
public String toString() { |
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) |
.append("weblogId", this.weblogId).append("text", this.text) |
.append("entryId", this.entryId) |
.append("version", this.version).append("categoryId", |
this.categoryId).append("category", this.category) |
.append("timeCreated", this.timeCreated).toString(); |
} |
} |
|
public void setWeblogId(Long weblogId) { |
this.weblogId = weblogId; |
} |
At line 984 removed 1 line. |
|
At line 987 changed 1 line. |
Weblog modifications: |
Add the following ''users'' property and accessor methods to ''Weblog.java''. |
At line 991 changed 1 line. |
package org.appfuse.model; |
private Set users = new HashSet(); |
|
/** |
* @hibernate.set table="weblog_user" cascade="none" lazy="false" |
* @hibernate.collection-key column="weblog_id" |
* @hibernate.collection-many-to-many class="org.appfuse.model.User" column="username" |
*/ |
public Set getUsers() { |
return users; |
} |
At line 993 changed 1 line. |
import java.util.Date; |
public void addUser(User user) { |
getUsers().add(user); |
} |
At line 995 changed 196 lines. |
import java.util.List; |
import java.util.HashSet; |
import java.util.Set; |
|
import org.apache.commons.lang.builder.EqualsBuilder; |
import org.apache.commons.lang.builder.HashCodeBuilder; |
import org.apache.commons.lang.builder.ToStringBuilder; |
import org.apache.commons.lang.builder.ToStringStyle; |
|
/** |
* @hibernate.class table="weblog" |
*/ |
public class Weblog extends BaseObject { |
|
private Long weblogId; |
private String username; |
private Date dateCreated; |
private String blogTitle; |
private Integer version; |
private List entries; |
private Set users = new HashSet(); |
|
/** |
* @return Returns the blogTitle. |
* |
* @hibernate.property column="blog_title" |
*/ |
public String getBlogTitle() { |
return blogTitle; |
} |
|
/** |
* @param blogTitle |
* The blogTitle to set. |
*/ |
public void setBlogTitle(String blogTitle) { |
this.blogTitle = blogTitle; |
} |
|
|
/** |
* @return Returns the dateCreated. |
* |
* @hibernate.property column="date_created" |
*/ |
public Date getDateCreated() { |
return dateCreated; |
} |
|
/** |
* @param dateCreated |
* The dateCreated to set. |
*/ |
public void setDateCreated(Date dateCreated) { |
this.dateCreated = dateCreated; |
} |
|
/** |
* @return Returns the entries. |
* |
* @hibernate.bag name="entries" lazy="true" inverse="true" cascade="delete" |
* @hibernate.collection-key column="entry_id" |
* @hibernate.collection-one-to-many class="org.appfuse.model.Entry" |
*/ |
public List getEntries() { |
return entries; |
} |
|
/** |
* @param entries |
* The entries to set. |
*/ |
public void setEntries(List entries) { |
this.entries = entries; |
} |
|
/** |
* @return Returns the id. |
* |
* @hibernate.id column="weblog_id" generator-class="native" |
* unsaved-value="null" |
*/ |
public Long getWeblogId() { |
return weblogId; |
} |
|
/** |
* @param id |
* The id to set. |
*/ |
public void setWeblogId(Long weblogId) { |
this.weblogId = weblogId; |
} |
|
/** |
* @return Returns the username. |
* |
* @hibernate.property column="username" |
*/ |
public String getUsername() { |
return username; |
} |
|
/** |
* @param username |
* The username to set. |
*/ |
public void setUsername(String username) { |
this.username = username; |
} |
|
/** |
* @return Returns the users. |
* |
* @hibernate.set table="weblog_user" cascade="none" lazy="false" |
* @hibernate.collection-key column="weblog_id" |
* @hibernate.collection-many-to-many class="org.appfuse.model.User" |
* column="username" |
*/ |
public Set getUsers() { |
return users; |
} |
|
/** |
* Adds a user for the weblog |
* |
* @param rolename |
*/ |
public void addUser(User user) { |
getUsers().add(user); |
} |
|
/** |
* @param users |
* The users to set. |
*/ |
public void setUsers(Set users) { |
this.users = users; |
} |
|
/** |
* @return Returns the version. |
* |
* @hibernate.version |
*/ |
public Integer getVersion() { |
return version; |
} |
|
/** |
* @param version |
* The version to set. |
*/ |
public void setVersion(Integer version) { |
this.version = version; |
} |
|
/** |
* @see java.lang.Object#equals(Object) |
*/ |
public boolean equals(Object object) { |
if (!(object instanceof Weblog)) { |
return false; |
} |
Weblog rhs = (Weblog) object; |
return new EqualsBuilder().append(this.users, rhs.users).append( |
this.blogTitle, rhs.blogTitle).append(this.weblogId, |
rhs.weblogId).append(this.username, rhs.username).append( |
this.entries, rhs.entries).append(this.dateCreated, |
rhs.dateCreated).append(this.version, rhs.version).isEquals(); |
} |
|
/** |
* @see java.lang.Object#hashCode() |
*/ |
public int hashCode() { |
return new HashCodeBuilder(1149273015, 1751732143).append(this.users) |
.append(this.blogTitle).append(this.weblogId).append( |
this.username).append(this.entries).append( |
this.dateCreated).append(this.version).toHashCode(); |
} |
|
/** |
* @see java.lang.Object#toString() |
*/ |
public String toString() { |
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) |
.append("weblogId", this.weblogId).append("version", |
this.version).append("users", this.users).append( |
"username", this.username).append("blogTitle", |
this.blogTitle).append("entries", this.entries).append( |
"dateCreated", this.dateCreated).toString(); |
} |
} |
|
|
public void setUsers(Set users) { |
this.users = users; |
} |
At line 151 added 2 lines. |
In addition to allow navigation from a Weblog object to their list of users, you need to add a Set of Weblogs to the User object - allowing that path of navigation as well. Modify ''User.java'' to add a ''weblogs'' Set and accessor methods. |
|
At line 1197 changed 1 line. |
package org.appfuse.model; |
protected Set weblogs; |
|
public Set getWeblogs() { |
return weblogs; |
} |
At line 1199 changed 430 lines. |
import java.io.Serializable; |
|
import java.util.ArrayList; |
import java.util.HashSet; |
import java.util.Iterator; |
import java.util.List; |
import java.util.Set; |
|
import org.apache.commons.lang.builder.EqualsBuilder; |
import org.apache.commons.lang.builder.HashCodeBuilder; |
import org.apache.commons.lang.builder.ToStringBuilder; |
import org.apache.commons.lang.builder.ToStringStyle; |
|
/** |
* User class |
* |
* This class is used to generate Spring Validation rules as well as the |
* Hibernate mapping file. |
* |
* <p> |
* <a href="User.java.html"> <i>View Source </i> </a> |
* </p> |
* |
* @author <a href="mailto:[email protected]">Matt Raible </a> Updated by |
* Dan Kibler ([email protected]) |
* |
* @hibernate.class table="app_user" |
*/ |
public class User extends BaseObject implements Serializable { |
protected String username; |
protected String password; |
protected String confirmPassword; |
protected String firstName; |
protected String lastName; |
protected Address address = new Address(); |
protected String phoneNumber; |
protected String email; |
protected String website; |
protected String passwordHint; |
protected Integer version; |
protected Boolean enabled; |
protected Set roles = new HashSet(); |
protected Set weblogs; |
|
/** |
* Returns the username. |
* |
* @return String |
* |
* @hibernate.id column="username" length="20" generator-class="assigned" |
* unsaved-value="version" |
*/ |
public String getUsername() { |
return username; |
} |
|
/** |
* Returns the password. |
* |
* @return String |
* |
* @hibernate.property column="password" not-null="true" |
*/ |
public String getPassword() { |
return password; |
} |
|
/** |
* Returns the confirmedPassword. |
* |
* @return String |
*/ |
public String getConfirmPassword() { |
return confirmPassword; |
} |
|
/** |
* Returns the firstName. |
* |
* @return String |
* |
* @hibernate.property column="first_name" not-null="true" length="50" |
*/ |
public String getFirstName() { |
return firstName; |
} |
|
/** |
* Returns the lastName. |
* |
* @return String |
* |
* @hibernate.property column="last_name" not-null="true" length="50" |
*/ |
public String getLastName() { |
return lastName; |
} |
|
public String getFullName() { |
return firstName + ' ' + lastName; |
} |
|
/** |
* Returns the address. |
* |
* @return Address |
* |
* @hibernate.component |
*/ |
public Address getAddress() { |
return address; |
} |
|
/** |
* Returns the email. This is an optional field for specifying a different |
* e-mail than the username. |
* |
* @return String |
* |
* @hibernate.property name="email" not-null="true" unique="true" |
*/ |
public String getEmail() { |
return email; |
} |
|
/** |
* Returns the phoneNumber. |
* |
* @return String |
* |
* @hibernate.property column="phone_number" not-null="false" |
*/ |
public String getPhoneNumber() { |
return phoneNumber; |
} |
|
/** |
* Returns the website. |
* |
* @return String |
* |
* @hibernate.property column="website" not-null="false" |
*/ |
public String getWebsite() { |
return website; |
} |
|
/** |
* Returns the passwordHint. |
* |
* @return String |
* |
* @hibernate.property column="password_hint" not-null="false" |
*/ |
public String getPasswordHint() { |
return passwordHint; |
} |
|
/** |
* Returns the user's roles. |
* |
* @return Set |
* |
* @hibernate.set table="user_role" cascade="save-update" lazy="false" |
* @hibernate.collection-key column="username" |
* @hibernate.collection-many-to-many class="org.appfuse.model.Role" |
* column="role_name" |
*/ |
public Set getRoles() { |
return roles; |
} |
|
/** |
* Adds a role for the user |
* |
* @param rolename |
*/ |
public void addRole(Role role) { |
getRoles().add(role); |
} |
|
/** |
* Sets the username. |
* |
* @param username |
* The username to set |
* @spring.validator type="required" |
*/ |
public void setUsername(String username) { |
this.username = username; |
} |
|
/** |
* Sets the password. |
* |
* @param password |
* The password to set |
* |
* @spring.validator type="required" |
* @spring.validator type="twofields" msgkey="errors.twofields" |
* @spring.validator-args arg1resource="user.password" |
* @spring.validator-args arg1resource="user.confirmPassword" |
* @spring.validator-var name="secondProperty" value="confirmPassword" |
*/ |
public void setPassword(String password) { |
this.password = password; |
} |
|
/** |
* Sets the confirmedPassword. |
* |
* @param confirmPassword |
* The confirmed password to set |
* @spring.validator type="required" |
*/ |
public void setConfirmPassword(String confirmPassword) { |
this.confirmPassword = confirmPassword; |
} |
|
/** |
* Sets the firstName. |
* |
* @spring.validator type="required" |
* @param firstName |
* The firstName to set |
*/ |
public void setFirstName(String firstName) { |
this.firstName = firstName; |
} |
|
/** |
* Sets the lastName. |
* |
* @param lastName |
* The lastName to set |
* |
* @spring.validator type="required" |
*/ |
public void setLastName(String lastName) { |
this.lastName = lastName; |
} |
|
/** |
* Sets the address. |
* |
* @param address |
* The address to set |
* |
* @spring.validator |
*/ |
public void setAddress(Address address) { |
this.address = address; |
} |
|
/** |
* Sets the email. |
* |
* @param email |
* The email to set |
* |
* @spring.validator type="required" |
* @spring.validator type="email" |
*/ |
public void setEmail(String email) { |
this.email = email; |
} |
|
/** |
* Sets the phoneNumber. |
* |
* @param phoneNumber |
* The phoneNumber to set |
* |
* @spring.validator type="mask" msgkey="errors.phone" |
* @spring.validator-var name="mask" value="${phone}" |
*/ |
public void setPhoneNumber(String phoneNumber) { |
this.phoneNumber = phoneNumber; |
} |
|
/** |
* Sets the website. |
* |
* @param website |
* The website to set |
*/ |
public void setWebsite(String website) { |
this.website = website; |
} |
|
/** |
* @param passwordHint |
* The password hint to set |
* |
* @spring.validator type="required" |
*/ |
public void setPasswordHint(String passwordHint) { |
this.passwordHint = passwordHint; |
} |
|
/** |
* Sets the roles. |
* |
* @param roles |
* The roles to set |
*/ |
public void setRoles(Set roles) { |
this.roles = roles; |
} |
|
/** |
* @return Returns the updated timestamp. |
* @hibernate.version |
*/ |
public Integer getVersion() { |
return version; |
} |
|
/** |
* @param updated |
* The updated version to set. |
*/ |
public void setVersion(Integer version) { |
this.version = version; |
} |
|
/** |
* @return Returns the weblogs. |
*/ |
public Set getWeblogs() { |
return weblogs; |
} |
|
/** |
* @param weblogs |
* The weblogs to set. |
*/ |
public void setWeblogs(Set weblogs) { |
this.weblogs = weblogs; |
} |
|
/** |
* @return Returns the enabled. |
* @hibernate.property column="enabled" |
*/ |
public Boolean getEnabled() { |
return enabled; |
} |
|
/** |
* @param enabled |
* The enabled to set. |
*/ |
public void setEnabled(Boolean enabled) { |
this.enabled = enabled; |
} |
|
/** |
* Convert user roles to LabelValue objects for convenience. |
*/ |
public List getRoleList() { |
List userRoles = new ArrayList(); |
|
if (this.roles != null) { |
for (Iterator it = roles.iterator(); it.hasNext();) { |
Role role = (Role) it.next(); |
|
// convert the user's roles to LabelValue Objects |
userRoles.add(new LabelValue(role.getName(), role.getName())); |
} |
} |
|
return userRoles; |
} |
|
/** |
* @see java.lang.Object#equals(Object) |
*/ |
public boolean equals(Object object) { |
if (!(object instanceof User)) { |
return false; |
} |
User rhs = (User) object; |
return new EqualsBuilder().append(this.password, rhs.password).append( |
this.passwordHint, rhs.passwordHint).append(this.address, |
rhs.address).append(this.roles, rhs.roles).append( |
this.firstName, rhs.firstName).append(this.lastName, |
rhs.lastName).append(this.version, rhs.version).append( |
this.weblogs, rhs.weblogs).append(this.enabled, rhs.enabled) |
.append(this.confirmPassword, rhs.confirmPassword).append( |
this.username, rhs.username).append(this.email, |
rhs.email).append(this.phoneNumber, rhs.phoneNumber) |
.append(this.website, rhs.website).isEquals(); |
} |
|
/** |
* @see java.lang.Object#hashCode() |
*/ |
public int hashCode() { |
return new HashCodeBuilder(-1892187061, -1292517047).append( |
this.password).append(this.passwordHint).append(this.address) |
.append(this.roles).append(this.firstName) |
.append(this.lastName).append(this.version) |
.append(this.weblogs).append(this.enabled).append( |
this.confirmPassword).append(this.username).append( |
this.email).append(this.phoneNumber).append( |
this.website).toHashCode(); |
} |
|
/** |
* @see java.lang.Object#toString() |
*/ |
public String toString() { |
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) |
.append("roles", this.roles).append("enabled", this.enabled) |
.append("firstName", this.firstName).append("lastName", |
this.lastName) |
.append("passwordHint", this.passwordHint).append("version", |
this.version).append("username", this.username).append( |
"weblogs", this.weblogs).append("fullName", |
this.getFullName()).append("email", this.email).append( |
"phoneNumber", this.phoneNumber).append("password", |
this.password).append("address", this.address).append( |
"confirmPassword", this.confirmPassword).append( |
"roleList", this.getRoleList()).append("website", |
this.website).toString(); |
} |
} |
|
|
public void setWeblogs(Set weblogs) { |
this.weblogs = weblogs; |
} |
At line 168 added 2 lines. |
In order to test that all of this works, you need to create a ''WeblogDaoTest.java'' class. Create this class in ''test/dao/**/dao'' and fill it with the following code. |
|
At line 1649 changed 1 line. |
|
|
At line 1652 changed 1 line. |
private UserDAO udao = null; |
private UserDAO udao = null; |
At line 1666 removed 9 lines. |
public void testGetWeblogInvalid() throws Exception { |
try { |
weblog = wldao.getWeblog(new Long(3000)); |
fail("weblogId found in database, failing test..."); |
} catch (DataAccessException d) { |
assertTrue(d != null); |
} |
} |
|
At line 1687 removed 1 line. |
|
At line 1701 changed 1 line. |
//add the same user twice - should result in no additional user |
// add the same user twice - should result in no additional user |
At line 1733 removed 1 line. |
|
At line 1735 removed 1 line. |
|
At line 1738 changed 1 line. |
!WeblogDAO |
This test relies on some sample data, so add the following XML to __metadata/sql/sample-data.xml__. |
At line 1740 changed 1 line. |
[{Java2HtmlPlugin |
<div style="color: blue !important; margin-left: 50px"> |
{{{ |
<table name='weblog'> |
<column>weblog_id</column> |
<column>blog_title</column> |
<column>date_created</column> |
<column>username</column> |
<row> |
<value>1</value> |
<value><![CDATA[Sponge Bob is Cool]]></value> |
<value>2004-03-31</value> |
<value>tomcat</value> |
</row> |
<row> |
<value>2</value> |
<value><![CDATA[Java Development = Fun]]></value> |
<value>2005-01-05</value> |
<value>mraible</value> |
</row> |
</table> |
<table name='weblog_user'> |
<column>weblog_id</column> |
<column>username</column> |
<row> |
<value>1</value> |
<value>tomcat</value> |
</row> |
<row> |
<value>1</value> |
<value>mraible</value> |
</row> |
<row> |
<value>2</value> |
<value>mraible</value> |
</row> |
</table> |
}}} |
</div> |
At line 304 added 1 line. |
Before this test will compile, you need to create the WeblogDAO interface and implementation. Create a ''WeblogDAO.java'' interface in src/dao/**/dao: |
At line 306 added 2 lines. |
[{Java2HtmlPlugin |
|
At line 1746 removed 1 line. |
|
At line 1750 changed 7 lines. |
|
public Weblog getWeblog(Long weblogId); |
|
public List getWeblogs(Weblog weblog); |
|
public void saveWeblog(Weblog weblog); |
|
public Weblog getWeblog(Long weblogId); |
public List getWeblogs(Weblog weblog); |
public void saveWeblog(Weblog weblog); |
At line 1758 removed 1 line. |
|
At line 1760 removed 1 line. |
|
At line 1763 removed 1 line. |
!WeblogDAOHibernate |
At line 322 added 2 lines. |
Then create the Hibernate implementation of this interface in the src/dao/**/dao/hibernate directory. |
|
At line 1775 removed 9 lines. |
/** |
* This class interacts with Spring's HibernateTemplate to save/delete and |
* retrieve Weblog objects. |
* |
* <p> |
* <a href="WeblogDAOHibernate.java.html"><i>View Source</i></a> |
* </p> |
* |
*/ |
At line 1787 changed 1 line. |
Weblog weblog = (Weblog) getHibernateTemplate().get(Weblog.class, weblogId); |
Weblog weblog = (Weblog) getHibernateTemplate().get(Weblog.class, weblogId); |
At line 1797 removed 3 lines. |
/** |
* @see org.appfuse.dao.WeblogDAO#getWeblogs(org.appfuse.model.Weblog) |
*/ |
At line 1804 removed 3 lines. |
/** |
* @see org.appfuse.dao.WeblogDAO#saveWeblog(org.appfuse.model.Weblog) |
*/ |
At line 1808 removed 4 lines. |
if (log.isDebugEnabled()) { |
log.debug("weblog's title: " + weblog.getBlogTitle()); |
} |
|
At line 1813 removed 2 lines. |
// necessary to throw a DataIntegrityViolation and catch it in WeblogManager |
getHibernateTemplate().flush(); |
At line 1817 removed 3 lines. |
/** |
* @see org.appfuse.dao.WeblogDAO#removeWeblog(java.lang.String) |
*/ |
At line 1821 changed 2 lines. |
Weblog weblog = getWeblog(weblogId); |
getHibernateTemplate().delete(weblog); |
getHibernateTemplate().delete(getWeblog(weblogId)); |
At line 1824 removed 1 line. |
|
At line 1826 removed 1 line. |
|
At line 1829 removed 47 lines. |
|
!Sample Data |
|
Add some sample data to /metadata/sql/sample-data.xml. |
|
<div style="color: blue !important; margin-left: 50px"> |
{{{ |
<table name='weblog'> |
<column>weblog_id</column> |
<column>blog_title</column> |
<column>date_created</column> |
<column>username</column> |
<column>version</column> |
<row> |
<value>1</value> |
<value><![CDATA[Sponge Bob is Cool]]></value> |
<value>2004-03-31</value> |
<value>tomcat</value> |
<value>1</value> |
</row> |
<row> |
<value>2</value> |
<value><![CDATA[Java Development = Fun]]></value> |
<value>2005-01-05</value> |
<value>mraible</value> |
<value>1</value> |
</row> |
</table> |
<table name='weblog_user'> |
<column>weblog_id</column> |
<column>username</column> |
<row> |
<value>1</value> |
<value>tomcat</value> |
</row> |
<row> |
<value>1</value> |
<value>mraible</value> |
</row> |
<row> |
<value>2</value> |
<value>mraible</value> |
</row> |
</table> |
}}} |
</div> |
|
At line 1877 removed 1 line. |
Modifications need to be made in applicationContext-hibernate.xml for the new entity Weblog. |
At line 363 added 2 lines. |
Modifications need to be made in applicationContext-hibernate.xml for the 3 new entities you created in this tutorial. |
|
At line 380 added 1 line. |
Also, you need to add the "weblogDAO" bean definition. |
At line 391 added 2 lines. |
After making these changes, you should be able to run __ant test-dao -Dtestcase=WeblogDAO__. All of the tests should pass successfully. |
|
At line 1907 removed 17 lines. |
|
|
<script type="text/javascript"> |
function toggleCode() { |
// javascript to expand collapse the large java listings |
divs = document.getElementsByTagName("div"); |
for (i=0; i < divs.length; i++) { |
if (divs[[i].getElementsByTagName("code").length > 0) { |
if (divs[[i].style.display == "") { |
divs[[i].style.display = "none"; |
} else { |
divs[[i].style.display = ""; |
} |
} |
} |
} |
</script> |