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.  [ 2 posts ] 
Author Message
 Post subject: Updating instead of insert
PostPosted: Mon Aug 18, 2008 3:31 pm 
Newbie

Joined: Mon Aug 18, 2008 1:07 pm
Posts: 3
I've been reading the forum about this problem and it seems prominent so hopefully someone can help. My table has a composite primary key of three elements. Two of the three elements are foreign keys to other tables. This is a large application and I can't really do any database changes so hopefully there's a solution. So this is how my hibernate file is set up:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true" assembly="blah.blah" namespace="blah.blah.blah">
<class name="StatutoryHoliday" table="T_StatutoryHoliday" lazy="true">

<composite-id>
<key-many-to-one name="Location" column="LocationId" class="blah.HumanResources.Location" />
<key-many-to-one name="StatutoryHolidayName" column="StatutoryHolidayId" class="blah.HumanResources.StatutoryHolidayName" />
<key-property name="CalendarYear" column="CalendarYear" type="String" />
</composite-id>

<!-- Properties -->
<property name="Holiday" column="Holiday" type="DateTime" />


</class >
</hibernate-mapping>

For the location class:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true" assembly="blah.Entity" namespace="blah.HumanResources">
<class name="Location" table="T_Location" lazy="true">

<!-- Primary Key -->
<id name="Id" column="LocationId" type="Int32" unsaved-value="0">
<generator class="native" />
</id >

<!-- Value Types -->
<property name="Name" column="Description" type="String" />
<property name="Active" column="Active" type="Boolean" />

</class >
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true" assembly="blah..Entity" namespace="blah.Entity.HumanResources">
<class name="StatutoryHolidayName" table="T_StatutoryHolidayName" lazy="true">

<!-- Primary Key -->
<id name="Id" column="StatutoryHolidayId" type="Int32" unsaved-value="0">
<generator class="native" />
</id >

<!-- Properties -->
<property column="Description" name="Description" type="String" not-null="true" />
</class >
</hibernate-mapping>

Now I think I know why it's doing the update instead of the insert and that's because all of the primary key values are set so something so it tries to do an update. I can put up the code I'm using if anyone wants to help. I'm also pretty new to hibernate and this is the first composite primary key I've had to set up so I may not even be doing that right. Any help would be appreciated. Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 19, 2008 9:45 am 
Newbie

Joined: Mon Aug 18, 2008 1:07 pm
Posts: 3
I fixed it by changing the table. I added a single primary key and making a unique constraint on the old fields.


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