Book.java
Code:
public class Book implements Serializable{
private String id;
private String name;
public Book() {
// TODO Auto-generated constructor stub
}
private List chapters;
public List getChapters() {
return chapters;
}
public void setChapters(List chapters) {
this.chapters = chapters;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Chapters.java
Code:
public class Chapter implements Serializable{
private String id;
private String name;
public Chapter() {
// TODO Auto-generated constructor stub
}
public Chapter(String id, String name) {
super();
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Mapping file
Code:
<hibernate-mapping default-cascade="all">
<class name="Book" table="BOOK" >
<id name="id" type="string" column="ID"></id>
<property name="name" type="string" column="NAME"></property>
<list name="chapters" cascade="save-update,delete" lazy="false" inverse="true" fetch="join">
<key column="BOOK_ID" update="true" ></key>
<list-index column="INDEXX" base="0" ></list-index>
<one-to-many class="Chapter" />
</list>
</class>
<class name="Chapter" table="CHAPTER">
<id name="id" type="string" column="ID"></id>
<property name="name" type="string" column="NAME"></property>
</class>
</hibernate-mapping>
Running code
Code:
public static void main(String[] args) {
try {
Chapter c1 = new Chapter("c1", "c1 name");
Chapter c2 = new Chapter("c2", "c2 name");
Chapter c3 = new Chapter("c3", "c3 name");
List cList = new ArrayList();
cList.add(c1);
cList.add(c2);
cList.add(c3);
Book b = new Book();
b.setId("b1");
b.setName("b name");
b.setChapters(cList);
Session s = getSessionFactory().openSession();
s.saveOrUpdate(b);
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
When i run the above code i need to persist Book with chapters...
sql statements executed by the class
Code:
Hibernate: select book_.ID, book_.NAME as NAME0_ from BOOK book_ where book_.ID=?
Hibernate: select chapter_.ID, chapter_.NAME as NAME1_ from CHAPTER chapter_ where chapter_.ID=?
Hibernate: select chapter_.ID, chapter_.NAME as NAME1_ from CHAPTER chapter_ where chapter_.ID=?
Hibernate: select chapter_.ID, chapter_.NAME as NAME1_ from CHAPTER chapter_ where chapter_.ID=?
it doesn't persist anything to the dadabase...
can anybody help me in this......it might be simple... but i cant able to figure out what is the problem...