Hi ,
Pls check my code and tell me Why Data is not inserted in Child table.Allthough It is insertion in Parent table sucessfully and no error is showing.
my Parent Class
::::::::::::;
package eg;
import java.util.Set;
public class Blog {
// private Long _id;
private String _id;
private String _name;
private Set _items;
public String getId() {
return _id;
}
public Set getItems() {
return _items;
}
public String getName() {
return _name;
}
public void setId(String long1) {
_id = long1;
}
public void setItems(Set list) {
_items = list;
}
public void setName(String string) {
_name = string;
}
}
::::::::::;
Child class
package eg;
import java.text.DateFormat;
import java.util.Calendar;
public class BlogItem {
// private Long _id;
private String _id;
// private Calendar _datetime;
private String _datetime;
private String _text;
private String _title;
private Blog _blog;
public Blog getBlog() {
return _blog;
}
public String getDatetime() {
return _datetime;
}
public String getId() {
return _id;
}
public String getText() {
return _text;
}
public String getTitle() {
return _title;
}
public void setBlog(Blog blog) {
_blog = blog;
}
public void setDatetime(String calendar) {
_datetime = calendar;
}
public void setId(String long1) {
_id = long1;
}
public void setText(String string) {
_text = string;
}
public void setTitle(String string) {
_title = string;
}
}
:::::::::::::::::;
parent Mapping
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="eg">
<class
name="Blog"
table="BLOGS"
lazy="true">
<id name="id" type="string" unsaved-value="null" >
<column name="BLOG_ID" sql-type="char(32)" not-null="true"/>
<generator class="assigned"/>
</id>
<property
name="name"
column="NAME"
not-null="true"
unique="true"/>
<set name="items" inverse="true" lazy="true" table="BLOG_ITEMS" >
<key column="BLOG_ID"/>
<one-to-many class="BlogItem"/>
</set>
</class>
</hibernate-mapping>
:::::::::::::::::::::::
child mapping
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="eg">
<class
name="BlogItem"
table="BLOG_ITEMS"
dynamic-update="true">
<!--
<class
name="BlogItem"
table="BLOG_ITEMS"
dynamic-update="true">
<id
name="id"
column="BLOG_ITEM_ID">
<generator class="assigned"/>
</id>
-->
<id name="id" type="string" unsaved-value="null" >
<column name="BLOG_ITEM_ID" sql-type="char(22)" not-null="true"/>
<generator class="native"/>
<!-- assigned-->
</id>
<property
name="title"
column="TITLE"
not-null="true"/>
<property
name="text"
column="TEXT"
not-null="true"/>
<property
name="datetime"
column="DATE_TIME"
not-null="true"/>
<many-to-one name="blog" class="Blog" column="BLOG_ID" not-null="true" cascade="save-update"/>
</class>
</hibernate-mapping>
Calling Code ::::::::::::::::::;
package eg;
import org.hibernate.*;
import org.hibernate.cfg.*;
import net.sf.hibernate.examples.quickstart.* ;
import java.util.*;
public class DMLBlogs
{
public DMLBlogs(){
System.out.println("Conatructor DMLCat Called ");
}
public void execute(){
Session session = HibernateUtil.currentSession();
Transaction tx= session.beginTransaction();
Blog b = new Blog();
b.setId("24");
System.out.println(" Now ID will set 24::::::::::::::;0");
b.setName("test blob:24");
Set s = new HashSet();
BlogItem i = new BlogItem();
System.out.println(" Now ID will set 24 ::::::::::::::;1");
i.setId("25");
i.setText("RD");
i.setTitle("Rad");
i.setDatetime("11");
i.setBlog(b);
s.add(i);
i = new BlogItem();
i.setBlog(b);
System.out.println(" Now ID will set 24:::::::::::::::;;2");
i.setId("26");
i.setTitle("TV01");
i.setText("Tele");
i.setDatetime("2");
s.add(i);
b.setItems(s);
session.save(b);
//session.save(i);
System.out.println(" After SAVE ::::::::::::Now ID will set 24:::::::::::::::;;2");
tx.commit();
HibernateUtil.closeSession();
}
}
With regards
Siddharth Singh
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
Mapping documents:
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
Name and version of the database you are using:
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt: