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?
01package entity;
02 
03import java.util.Date;
04import java.util.HashSet;
05import java.util.Set;
06 
07import javax.persistence.Entity;
08import javax.persistence.GeneratedValue;
09import javax.persistence.Id;
10 
11@Entity
12public class Keyword{
13  
14 @Id
15 @GeneratedValue
16 private Integer keywordId;
17 private String title;
18 private String body;
19 private Set<keyword> parents = new HashSet<keyword>();
20 private Set<keyword> children = new HashSet<keyword>();
21 private Date lastModifiedTime;
22 private Date createTime;
23 public Integer getKeywordId() {
24  return keywordId;
25 }
26 public void setKeywordId(Integer keywordId) {
27  this.keywordId = keywordId;
28 }
29 public String getTitle() {
30  return title;
31 }
32 public void setTitle(String title) {
33  this.title = title;
34 }
35 public String getBody() {
36  return body;
37 }
38 public void setBody(String body) {
39  this.body = body;
40 }
41 public Set<keyword> getParents() {
42  return parents;
43 }
44 public void setParents(Set<keyword> parents) {
45  this.parents = parents;
46 }
47 public Set<keyword> getChildren() {
48  return children;
49 }
50 public void setChildren(Set<keyword> children) {
51  this.children = children;
52 }
53 public Date getLastModifiedTime() {
54  return lastModifiedTime;
55 }
56 public void setLastModifiedTime(Date lastModifiedTime) {
57  this.lastModifiedTime = lastModifiedTime;
58 }
59 public Date getCreateTime() {
60  return createTime;
61 }
62 public void setCreateTime(Date createTime) {
63  this.createTime = createTime;
64 }
65}
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
ADVERTISING WITH US - Direct your advertising requests to Michael