-->
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: Composite-id problem: updating instead of inserting fields
PostPosted: Mon Mar 15, 2004 10:58 pm 
Newbie

Joined: Fri Dec 19, 2003 1:31 pm
Posts: 12
I have a table of Employees and a table of Groups.
An employee may belong to any number of groups so there is a many-to-many relationship in there. Also i need the date to know when the employee got into the group, so the schema is something like this:

Employee
idEmployee (PK)
salary
dob

---

Group
idGroup (PK)
name

---

GroupEmployee
idEmployee (PK, FK)
idGroup (PK, FK)
date

so, i was trying to map this ussing Hibernate (2.1) and Oracle 9i database.
this are the mappings i have

Code:
  <class name="com.pba.GroupEmployee" table="GROUPEMPLOYEE">
    <composite-id>
      <key-property name="idEmployee" column="IDEMPLOYEE" type="integer"/>
      <key-property name="idGroup" column="IDGROUP" type="integer"/>
    </composite-id>
    <property name="date" column="DATE" type="java.util.Date" />
  </class>

  <class name="com.pba.Employee" table="EMPLOYEE">
    <id name="idEmployee" column="IDEMPLOYEE" type="integer" unsaved-value="null" >
      <generator class="sequence">
        <param name="sequence">EMP_SEQ</param>
      </generator>
    </id>
<property name="dob" column="DOB" type="java.util.Date"/>
<property name="salary" column="SALARY" type="float"/>
    <set name="groups" table="GROUPEMPLOYEE">
      <key column="IDEMPLOYEE"/>
      <one-to-many class="com.pba.GROUPEMPLOYEE"/>
    </set>
</class>

  <class name="com.pba.Group" table="GROUP">
    <id name="idGroup" column="IDGROUP" type="integer" unsaved-value="null" >
      <generator class="sequence">
        <param name="sequence">GRP_SEQ</param>
      </generator>
    </id>
    <property name="name" column="NAME" type="string"/>
  </class>


Then i'm trying to insert an employee with a set of 3 groups, but it never inserts anything on the GROUPEMPLOYEE table.
The generated sql goes like (in this case i insert the employee first and then i add the groups to it...)

Code:
Hibernate: update EMPLOYEE set ... {All employee data goes here}
Hibernate: update GROUPEMPLOYEE set date=? where IDEMPLOYEE=? and IDGROUP=?
Hibernate: update GROUPEMPLOYEE set IDEMPLOYEE=? where IDEMPLOYEE=? and IDGROUP=?


How can it do it then to be able to insert the data in the GROUEMPLOYEE table ??

Thanx


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 2:55 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
http://www.hibernate.org/hib_docs/refer ... child.html


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 4:28 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
http://www.hibernate.org/116.html#A11
Ithink you need to introduce version property in the GroupEmployee
Quote:
In Hibernate 2.1, the best solution is to set unsaved-value for the <version> or <timestamp> element, if your data is versioned.

GroupEmployee has composite-id with assigned values, hibernate need some hint (version or timestamp property) to understand what it has to do, insert or update.

jorxferx, you cand download my junit test for version bug (literal int version is not incremented)
http://opensource.atlassian.com/projects/hibernate/secure/attachment/10500/IntVersionTest-src.zip
In hibernate 2.1.2 this bug is fixed and you can use my test like example. If the link is broken, download IntVersionTest-src.zip from http://opensource.atlassian.com/projects/hibernate/secure/ViewIssue.jspa?key=HB-626

Leonid


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 5:26 pm 
Newbie

Joined: Fri Dec 19, 2003 1:31 pm
Posts: 12
Thanx for your help guys..

That really help me a lot solving my issue.


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.