-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: use set represent parent/child relationship
PostPosted: Thu Nov 18, 2004 6:29 am 
Regular
Regular

Joined: Fri Nov 12, 2004 12:07 am
Posts: 57
Location: Beijing,China
Hi,

I am newbie,one problem always bothers my head recently,that is using set tag represent parent/child
relationship. before use set tag i always used bag tag, it worked fine,but effect was not good. so i
decide to use set tag to represent parent/child relationship for effect. but the result is not what i
expected,can anybody tell me what my miss is?
Thanks in advance.

class Parent implements Serialiable{
private int id;
private Set children=new TreeSet();

public int getId(){
return id;
}

public void setId(int id){
this.id=id;
}

public int getChildren(){
return children;
}

public void setChildren(Set children){
this.children=children;
}

public toString equals(Object other){
.........
}

public String toString(){
........
}

public int hashCode(){
........
}

}

class Child implements Serializable,Comparable{
private int id;
private String name=null;
private Parent parent=null;

public int getId(){
return id;
}

public void setId(int id){
this.id=id;
}

public String getName(){
return name;
}

public void setName(String name){
this.name=name;
}

public Parent getParent(){
return parent;
}

public void setParent(Parent parent){
this.parent=parent;
}

public toString equals(Object other){
.........
}

public String toString(){
........
}

public int hashCode(){
........
}

public int compareTo(Object obj){
return -1;
}

}

Parent.xml
.......
<id name="id" column="id" type="int" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="name" column="name" type="string" length="100"/>
<set name="Childs" inverse="false" cascade="all" lazy="true" sort="natural">
<key column="collgeId"/>
<one-to-many class="bean.Child"/>
</set>
.............

Child.xml
.............
<id name="id" column="id" type="int" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="name" column="name" type="string" length="100"/>
<many-to-one name="parent" class="bean.Parent" column="collgeId" not-null="true"/>
..................

code:

session=sessionFactory.openSession();
Parent p=(Parent)session.get(Parent.class,new Integer(1));
Set set=p.getChildren();

Child c1=new Child();
c1.setName("Tom 1");
c1.setParent(c);
set.add(c1);

Child c2=new Child();
c2.setName("Tom 2");
c2.setParent(c);
set.add(c2);

session.flush();
session.close();

My DB is MySql4.0,PlatForm is WinXP.

1.When set tag in parent.xml has attribute sort="natural", result as below:

after executed 1 time:

id name parent_id
1 Tom 1 1
2 Tom 2 1

after executed 2 times:

id name parent_id
1 Tom 1 Null
2 Tom 2 Null
3 Tom 1 1
4 Tom 2 1

why is this?

2.when set hasn't sort="natural", result as below:

after executed 1 time:

id name parent_id
1 Tom 1 1

after executed 2 times:

id name parent_id
1 Tom 1 1
2 Tom 1 1


only the first record is inserted,why?

any help for appreciate.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 6:50 am 
Regular
Regular

Joined: Fri Nov 12, 2004 12:07 am
Posts: 57
Location: Beijing,China
could anyone please tell me which concrete class implemented Set is used by you to represent simpler one-to-many relation in hibernate ?

any help is appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 9:11 am 
Beginner
Beginner

Joined: Sat Nov 13, 2004 7:21 pm
Posts: 24
Hi

try to use inverse="true" but I'm not sure.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 9:37 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Just follow the "Parent/Child relationship" chapter in the reference documentation step by step.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 2:50 pm 
Regular
Regular

Joined: Fri Jul 16, 2004 3:04 pm
Posts: 52
Location: Wiltshire UK
The problem is possibly with your equals() & hashcode methods. Take a look at this link here & see if it helps http://www.hibernate.org/109.html.

For choosing concrete implementations of Set see this page to help you decide which you need.
http://java.sun.com/developer/JDCTechTips/2002/tt1105.html

HTH
Paul :-)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 20, 2004 3:23 am 
Regular
Regular

Joined: Fri Nov 12, 2004 12:07 am
Posts: 57
Location: Beijing,China
Ho,it works fine!
A lot of thanks for funky,christian and javacoda.I overwrote equals() and hashCode() method
as generated by ddl2hbm,it works fine whether inverse is true or not.The two articles provided
by javacoda are very excellent for me.

You are my hero.I will introduce my nice sister to you if i have.haha

BTW,what's meaning about sort="natural"?

when inverse="false" and sort="natural",the case as below

id name parent_id
1 Tom 1 Null
2 Tom 2 Null
3 Tom 1 1
4 Tom 2 1

is still exist.
thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 22, 2004 6:09 am 
Regular
Regular

Joined: Fri Nov 12, 2004 12:07 am
Posts: 57
Location: Beijing,China
Hi guys,


Who encountered this circumstance that attribute sort="natural" in set tag recently or in the past and what is that men?

I'll very appreciated for your any help and post,even only a look!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 22, 2004 6:09 am 
Regular
Regular

Joined: Fri Nov 12, 2004 12:07 am
Posts: 57
Location: Beijing,China
Hi guys,


Who encountered this circumstance that attribute sort="natural" in set tag recently or in the past and what is that mean?

I'll very appreciated for your any help and post,even only a look!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 23, 2004 3:56 pm 
Regular
Regular

Joined: Fri Jul 16, 2004 3:04 pm
Posts: 52
Location: Wiltshire UK
http://www.hibernate.org/hib_docs/reference/en/html_single/#collections-sorted

Paul :-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.