Hi,
I am new to Hibernate. I am trying to implement Uni-directional mappings by using two class Parent and Child.
package com.sen;
import java.util.Set;
package com.sen;
import java.util.Set;
public class Parent {
private long id;
private Set Children;
public Set getChildren() {return Children;}
public void setChildren(Set children) { Children = children;}
public long getId() {return id; }
public void setId(long id) {this.id = id;}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.sen.Parent" table="parent">
<id name="id" type="long" column="pid"/>
<!-- <generator class="native" />
</id> -->
<set name="children" inverse="false" cascade="none">
<key column="pid" />
<one-to-many column="cid" class="com.sen.Child" />
</set>
</class>
</hibernate-mapping>
-----------------------------
package com.sen;
package com.sen;
public class Child {
private long id;
private String firstName;
private String lastName;
public String getFirstName() {return firstName; }
public void setFirstName(String firstName) {this.firstName = firstName; }
public long getId() {return id; }
public void setId(long id) {this.id = id;}
public String getLastName() {return lastName;}
public void setLastName(String lastName) {this.lastName = lastName;}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.sen.Child" table="child">
<id name="id" type="long" column="cid" />
<!-- <generator class="native" />
</id> -->
<property name="firstName" column="firstName" />
<property name="lastName" column="lastName" />
</class>
</hibernate-mapping>
------------------------
The client program for the same as given below.
package com.sen; import java.io.*; import java.util.*; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration;
public class AssociationClient { public static void main(String ar[]){
try{
System.out.println("11111111"); File f=new File("D:\\sen\\springapp\\src\\com\\association\\hibernate.cfg.xml"); System.out.println("f.getAbsolutePath()==>"+f.getAbsolutePath()); Configuration cfg = new Configuration().addFile(f); System.out.println("22222222"); SessionFactory sessionFactory = cfg.configure().buildSessionFactory(); Session session =sessionFactory.openSession(); System.out.println("33333333"); Transaction transaction = session.beginTransaction(); //List list = query.list(); //The insertion to be done here. //Parent set up. Parent parent = new Parent(); System.out.println("4444444"); //create child 1. Child child1 = new Child(); child1.setFirstName("John"); child1.setLastName("Smith");
// create the set of children. Set children = new HashSet(); children.add(child1); System.out.println("555555"); // create child 2. /*Child child2 = new Child(); child2.setFirstName("John"); child2.setLastName("Wright"); children.add(child2);*/ parent.setChildren(children); System.out.println("6666666"); session.saveOrUpdate(child1); System.out.println("777777"); session.saveOrUpdate(parent); transaction.commit();
}catch(Exception e){ System.out.println(e.getMessage()); System.out.println(e); } } }
-----------------------
I am getting the following output.
11111111 f.getAbsolutePath()==>D:\sen\springapp\src\com\association\hibernate.cfg.xml 33333333 4444444 555555 6666666 Hibernate: select child_.cid, child_.firstName as firstName1_, child_.lastName as lastName1_ from child child_ where child_.cid=? could not retrieve snapshot: [com.sen.Child#0] org.hibernate.exception.GenericJDBCException: could not retrieve snapshot: [com.sen.Child#0]
-----------------------
Kindly help me in this regard.
|