-->
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.  [ 6 posts ] 
Author Message
 Post subject: newbie: how to save a set
PostPosted: Sat Dec 18, 2004 7:23 am 
Newbie

Joined: Wed Nov 24, 2004 2:44 pm
Posts: 13
Hallo,

I have a class Person with the sets Address and CommunicationInfo. If I save a Person, I get just UPDATEs for the sets instead of INSERTS:

INFO: schema export complete
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Person (Client, Surname, Maidenname, Forename, Title, Martialstatus, Religion, Birthday, Placeofbirth, Socialsecuritynumber, Spokenlanguages, "Comment", id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: update CommunicationInfo set client=?, "Comment"=?, ConnectionType=?, Connection=? where id=?
Hibernate: update Address set id=? where id=?
Hibernate: update Address set id=? where id=?

The Person.hbm.xml file is:

<?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="ena.bl">

<class name="Person" table="Person">
<id name="id">
<generator class="native"/>
</id>
<property name="Client"/>

<property name="Surname"/>
<property name="Maidenname"/>
<property name="Forename"/>
<property name="Title"/>
<property name="Martialstatus"/>
<property name="Religion"/>
<property name="Birthday"/>
<property name="Placeofbirth"/>
<property name="Socialsecuritynumber"/>
<property name="Spokenlanguages"/>
<property name="Comment" column="`COMMENT`"/>

<set
name="Address">
<key column="owner_id"/>
<one-to-many class="ena.bl.Address"/>
</set>

<set
name="CommunicationInfo"
<key column="owner_id"/>
<one-to-many class="ena.bl.CommunicationInfo"/>
</set>



</class>

</hibernate-mapping>


Any help? Thank you!

Richard


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 18, 2004 11:17 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
eh - if the address can be updated it must have been inserted previously!
(and you don't show what code you are actually executing)

this stuff is described in many tutorials, the docs and examples - take one of those and see that work and work out from there ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 19, 2004 1:25 pm 
Newbie

Joined: Wed Nov 24, 2004 2:44 pm
Posts: 13
> this stuff is described in many tutorials, the docs and examples - take one of those and see that work and work out from there ;)

Max, that is what I try. However, I’m a little bit confused with the one-to-many mappings. It doesn’t what I want. Now I have a problem with “cascade”. If I save a Person l, the collections aren’t saved. I got a duplicate key error for the 2nd address.

l.setForename("Lucky");
l.setSurname("Luke");

Ci.setComment("bla bla ");
Adr1.setCity("Braunschweig");
Adr2.setCity("Hildesheim");

l.addAddress(Adr1);
l.addAddress(Adr2);
l.addCommunicationInfo(Ci);

// with this three lines it's working ...
//s.save(Adr1);
//s.save(Adr2);
//s.save(Ci);
s.save(l);

Here the hbm.xml file:

<?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="ena.bl">

<class name="Person" table="Person">
<id name="id">
<generator class="native"/>
</id>
<property name="Client"/>

<property name="Surname"/>
<property name="Maidenname"/>
<property name="Forename"/>
<property name="Title"/>
<property name="Martialstatus"/>
<property name="Religion"/>
<property name="Birthday"/>
<property name="Placeofbirth"/>
<property name="Socialsecuritynumber"/>
<property name="Spokenlanguages"/>
<property name="Comment" column="`Comment`"/>

<set
name="Address"
cascade="all">
<key column="owner_id"/>
<one-to-many class="ena.bl.Address"/>
</set>

<set
name="CommunicationInfo"
cascade="all">
<key column="owner_id"/>
<one-to-many class="ena.bl.CommunicationInfo"/>
</set>

</class>

</hibernate-mapping>

What is missing that hibernate gives the addresses a new id? Must I write something in the mapping file for the address class? Thank you! (It must be very easy … But I can’t see the forest because of the trees.

Richard


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 19, 2004 1:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
does you address know it's Person ?

if yes - then remember to set inverse to false|true the right places (read docs about inverse if you don't know about it ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 19, 2004 2:28 pm 
Newbie

Joined: Wed Nov 24, 2004 2:44 pm
Posts: 13
No, neither my Java-Address-Code nor my Address-mapping file know anything about Person. Just the Person knows about the Addresses.
How can I introduce the Person to the Addresses? Thank you!

Regards Richard


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 19, 2004 3:38 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
i really can't come up with better explanations than already said and done in faqs, examples, dcos etc.

Please take one of those and follow - and see it work!

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.