Joined: Tue Jan 20, 2009 12:31 am Posts: 1
|
hi guys...
i m trying <one-to-many > mapping between team and members...
insertion in db works fine but when i retrieve team members value are not comming...i m pasting the code....
member.hbm.xml -
<?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="member" table="member1">
<id name="id" column="id" unsaved-value="0">
<generator class="native"></generator>
</id>
<property name="info" type="string"/>
</class>
</hibernate-mapping>
team.hbm.xml -
<?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="team" table="team1">
<id name="ID" column="id" unsaved-value="0">
<generator class="native"></generator>
</id>
<list name="members" cascade="all">
<key column="parent_id"/>
<index column="idx"/>
<one-to-many class="member"/>
</list>
<property name="name" type="string"/>
</class>
</hibernate-mapping>
/**
*
* @author moiaz- member.java
*/
public class member {
private String info;
private int id;
public member() {
}
public member(String d){
setInfo(d);
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
team.java
import java.util.List;
public class team {
private String name;
private List members;
private int ID;
public team() { }
public team(String d)
{
setName(d);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getMembers() {
return members;
}
public void setMembers(List members) {
this.members = members;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
}
main program
public class One2ManyTest {
public static void main(String[] args) {
try{
/* SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session=sf.openSession();*/
Configuration cfg= new Configuration();
//cfg.addClass(team.class);
//cfg.addClass(member.class);
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
team team1=new team("Australia");
ArrayList list1=new ArrayList();
list1.add(new member("ricky"));
list1.add(new member("mikky"));
list1.add(new member("tricky"));
team1.setMembers(list1);
session.save(team1);
tx.commit();
session.flush();
session.close();
System.out.println("added");
}
catch(Exception e1)
{
// System.out.println(""+e1);
e1.printStackTrace();
}
}
}
this insertion works fine...
but when i do this code -
public class One2ManyTest {
public static void main(String[] args) {
try{
/* SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session=sf.openSession();*/
Configuration cfg= new Configuration();
//cfg.addClass(team.class);
//cfg.addClass(member.class);
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
team t = (team)session.get(team.class, new Integer(2));
}}
it gives null in the member arraylist...
can anyone plz help me :(
_________________ mOIAz
|
|