-->
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.  [ 4 posts ] 
Author Message
 Post subject: Use of bidirectional assosiation with composite key
PostPosted: Fri Mar 11, 2005 7:37 am 
Newbie

Joined: Wed Mar 09, 2005 5:03 am
Posts: 7
Hibernate does not work correctly in case a set references to a
class with composite id. It issues not an insert, but update statement for the referenced class.

Note: in case of simple PKs hibernate works fine (see alternative sections below).

Hibernate version:
2.1.8 / 3.0 rc1
Mapping documents:
<class name="Server" table="SERVER">
<id name="serverId" type="java.lang.Integer" column="SERVER_ID">
<generator class="sequence">
<param name="sequence">fmsmgr.SERVER_ID_SEQ</param>
</generator>
</id>

<property name="url" column="URL" type="java.lang.String" not-null="true" unique="true" length="256" />
<property name="createDt" column="CREATE_DT"
type="java.util.Date" insert="false" update="false" />
<property name="modDt" column="MOD_DT"
type="java.util.Date" insert="false" update="false" />

<set name="attributes" inverse="false" cascade="all-delete-orphan">
<key>
<column name="SERVER_ID" />
</key>
<one-to-many class="ServerAttribute" />
</set>

</class>

<class name="ServerAttribute" table="SERVER_ATTRIBUTE">

<composite-id name="PK" class="ServerAttributePK">
<key-property name="serverId" column="SERVER_ID" type="java.lang.Integer" length="6" />
<key-property name="name" column="NAME" type="java.lang.String" length="100"/>
</composite-id>

<property name="value" column="VALUE" type="java.lang.String" length="256" />
<property name="createDt" column="CREATE_DT" type="java.util.Date" insert="false" update="false" />
<property name="modDt" column="MOD_DT" type="java.util.Date" insert="false" update="false" />

<many-to-one name="server" column="SERVER_ID" class="Server" insert="false" update="false" not-null="true" />
</class>

Alternative mapping
<class name="ServerAttribute" table="SERVER_ATTRIBUTE">
<id name="id" type="java.lang.Integer" column="SERVER_ATTRIBUTE_ID">
<generator class="sequence">
<param name="sequence">fmsmgr.SERVER_ID_SEQ</param>
</generator>
</id-->

<property name="value" column="VALUE" type="java.lang.String" length="256" />
<property name="createDt" column="CREATE_DT" type="java.util.Date" insert="false" update="false" />
<property name="modDt" column="MOD_DT" type="java.util.Date" insert="false" update="false" />

<many-to-one name="server" column="SERVER_ID" class="Server" insert="false" update="false" not-null="true" />
</class>
Code between sessionFactory.openSession() and session.close():
Server server = new Server();
server.setUrl("sadasdsada");
ServerAttribute attribute = new ServerAttribute(new ServerAttributePK(null, "name"));
attribute.setValue("bsdsadsa");
attribute.setServer(server);
server.getAttributes().add(attribute);
session.save(server);
session.flush();

Alternative code
Server server = new Server();
server.setUrl("sadasdsada");
ServerAttribute attribute = new ServerAttribute();
attribute.setValue("bsdsadsa");
attribute.setServer(server);
server.getAttributes().add(attribute);
session.save(server);
session.flush();

The generated SQL (show_sql=true):

insert into SERVER (URL, SERVER_ID) values (?, ?)
update SERVER_ATTRIBUTE set VALUE=? where SERVER_ID=? and NAME=?
update SERVER_ATTRIBUTE set SERVER_ID=? where SERVER_ID=? and NAME=?

Alternative sql
insert into SERVER (URL, SERVER_ID) values (?, ?)
insert into SERVER_ATTRIBUTE (VALUE, SERVER_ATTRIBUTE_ID) values (?, ?)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 8:38 am 
Newbie

Joined: Wed Mar 09, 2005 5:03 am
Posts: 7
Can anyone give me a working example of parent/child with composite ids?
There is no such example in hibernate in action book, all bidirectional one-to-many associations have a simple id, not a composite.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 9:04 am 
Newbie

Joined: Thu Mar 17, 2005 7:50 am
Posts: 8
Location: Tunis
http://www.hibernate.org/hib_docs/v3/re ... pings.html

But still did not work for me :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 1:41 pm 
Newbie

Joined: Thu Nov 18, 2004 4:03 pm
Posts: 7
Location: Pittsburgh,pa
You may want to check all the types you have mapped to your columns. Have seen the same thing when Integer should be mapped as a string etc


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