Follow this Blog!

Oct 13, 2010

f Comment

Hibernate Self Join - Create Java Class

MenuHow to Do Self Join Many-To-Many Mapping In Hibernate
Hibernate Self Join - Create DB Tables
Hibernate Self Join - Create Java Class
Hibernate Self Join - Create Hibernate Configuration
Hibernate Self Join - Test Java Code
Amazon Now we have the database tables to represent our entity Keyword and to join it to itself, let's create a Java class to embody a Keyword and its self-join characteristic. Note that you need to have two Sets of Keywords as the instance variables: one as its parents and one as its children. Then create simple setters and getters of each property. Easy right? Questions?
package entity;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Keyword{
 
 @Id
 @GeneratedValue
 private Integer keywordId;
 private String title;
 private String body;
 private Set<keyword> parents = new HashSet<keyword>();
 private Set<keyword> children = new HashSet<keyword>();
 private Date lastModifiedTime;
 private Date createTime;
 public Integer getKeywordId() {
  return keywordId;
 }
 public void setKeywordId(Integer keywordId) {
  this.keywordId = keywordId;
 }
 public String getTitle() {
  return title;
 }
 public void setTitle(String title) {
  this.title = title;
 }
 public String getBody() {
  return body;
 }
 public void setBody(String body) {
  this.body = body;
 }
 public Set<keyword> getParents() {
  return parents;
 }
 public void setParents(Set<keyword> parents) {
  this.parents = parents;
 }
 public Set<keyword> getChildren() {
  return children;
 }
 public void setChildren(Set<keyword> children) {
  this.children = children;
 }
 public Date getLastModifiedTime() {
  return lastModifiedTime;
 }
 public void setLastModifiedTime(Date lastModifiedTime) {
  this.lastModifiedTime = lastModifiedTime;
 }
 public Date getCreateTime() {
  return createTime;
 }
 public void setCreateTime(Date createTime) {
  this.createTime = createTime;
 }
}
Let's create the necessary Hibernate configurations to let Hibernate know all about our little plan to have Keyword able to join itself! Questions? Let me know!

◀ Create DB TablesCreate Hibernate Configuration ▶
Please leave a comment here!
One Minute Information - by Michael Wen
Find Michael on Google or Facebook
ADVERTISING WITH US - Direct your advertising requests to Michael