HibernateRelationships |
|
Your trail: |
This is version 32.
It is not the current version, and thus it cannot be edited.
[Back to current version]
[Restore this version]
About this tutorial
This is a tutorial to show how to create and manage Hibernate relationships within AppFuse. This tutorial was written for AppFuse 1.8 and the AppGen pieces may not work with previous versions.
- 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.
Table of Contents
- Create Weblog.java, Entry.java and add XDoclet tags
- [Many-to-One] Create a new Category object and modify Entry.java to use it
- [One-to-Many] A Weblog object can have many Entries
- [Many-to-Many] ???
- Lazy-Loading Issues
- Managing relationships and indexed properties in the UI
Create Weblog.java, Entry.java and add XDoclet tags
For our purposes, the Weblog entity is a list of available weblogs. A Weblog object has the following properties an id, a username, 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.
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="weblog"
*/
public class Weblog extends BaseObject {
private Long weblogId;
private String username;
private Date dateCreated;
private String blogTitle;
private Integer version;
/**
* @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 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();
}
}
|
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();
}
}
|
[Many-to-One] Create a new Category object and modify Entry.java to use it
A category object will act as an entity for persisting category information about weblog entries. A category object will consist of an id (primary key), category name, and a description. Each category can have many entries.
package org.appfuse.model;
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 relatioships 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.
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 entryId;
private String text;
private Date timeCreated;
private Integer version;
private Long categoryId;
private Category category;
/*
* 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();
}
}
|
[One-to-Many] A Weblog object can have many Entries
package org.appfuse.model;
import java.util.Date;
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();
}
}
|
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 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();
}
}
|
[Many-to-Many] ???
Lazy-Loading Issues
Managing relationships and indexed properties in the UI
Attachments:
|