-->
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.  [ 13 posts ] 
Author Message
 Post subject: Problem with assigned id's (?)
PostPosted: Wed Mar 12, 2008 9:45 am 
Beginner
Beginner

Joined: Fri Aug 10, 2007 3:34 am
Posts: 44
I've recently changed all my mappings to support assigned id's
since then, I've had many problems.

I red the documentation and applied what it said.

still, something bad happens when I'm saving a new object:
an association is updated instead of being inserted.

what could be the problem?

i will post the mappings later (can't now)

p.s. : sorry for bad English grammar/spelling :D


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 12, 2008 10:13 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
What parts of the documentation have you applied ?

http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/example-parentchild.html#example-parentchild-update

This one ? I suppose hibernate can't detect if the associated object is new or not. But without mappings and code it's hard to say where the real problem is.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 12, 2008 4:36 pm 
Beginner
Beginner

Joined: Fri Aug 10, 2007 3:34 am
Posts: 44
hy wolli ^^

here's the mapping

the updating occurs when i call session.Save(obj) where obj
is of SupplierDTO type

<class name="PJDTO" table="PJ" lazy="false" >
<id name="Id" column="Id" type="Int32" >
<generator class="assigned" />
</id>
<version name="Ver"/>
... here are some mapped propertyes
<bag name="BankAccounts" inverse="true" cascade="all-delete-orphan" lazy="false" >
<key column="PJId"/>
<one-to-many class="BankAccountDTO"/>
</bag>
</class>

<joined-subclass name="PartenerDTO" table="Parteners" extends="PJDTO" lazy="false">
<key column="Id"/>
... mapped propertyes again

<bag name="Locations" inverse="true" cascade="all-delete-orphan" lazy="false">
<key column="PartenerId"/>
<one-to-many class="PartenerLocationDTO"/>
</bag>
</joined-subclass>

<joined-subclass name="SupplierDTO" table="Suppliers" extends="PartenerDTO" lazy="false">
<key column="Id"/>
</joined-subclass>

the PartnerLocationDTO mapping:
<class name="LocationDTO" table="Locations" lazy="false" >
<id name="Id" column="Id" type="Int32" >
<generator class="assigned" />
</id>
<property name="UID" column="UID"/>
<property name="Name" column="Name" type="String" length="100"/>
<many-to-one name="ContactData" column="ContacDatatId" class="JS.NC.DTOs.Nom.DateContact.ContactDataDTO" cascade="all" />
</class>

<joined-subclass name="PartenerLocationDTO" table="PartenerLocation" extends="LocationDTO" lazy="false">
<key column="Id"/>

<bag name="ContactP" inverse="true" cascade="all-delete-orphan" lazy="false" >
<key column="PartenerLocId"/>
<one-to-many class="JS.NC.DTOs.Nom.PersoaneFizice.ContactPDTO"/>
</bag>

<many-to-one name="Parent" column="PartenerId" class="PartenerDTO" not-null="true" update="true"/>
</joined-subclass>


i tried to remove other mapping components of PartnerDTO leaving only the Locations bag, and the same thing still happens.

id there any problem with the mapping? Cause if there isn't
there must be a problem in the application

hope the mappings aren't confusing

thanks :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 13, 2008 3:20 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I'm guessing ... there's no version or timestamp mapping in LocationDTO, so hibernate can't figure out, which of the bag's item is new.

I also work with my own id's, but instead of "assigned", I've written my own generator class (which is quite simple).

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 13, 2008 3:43 am 
Beginner
Beginner

Joined: Fri Aug 10, 2007 3:34 am
Posts: 44
are you saying that i have to create version or timestamp for assigned ids?
i also use an id generator (gets the last id from DB and increments it)
does nhibernate provide an interface for id generators? if so, should i use
that instead of mine?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 13, 2008 5:16 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Have a look at the documentation url I posted in the beginning. According to that, you have to go down that road if you want cascading to work. I'm using the hibernate interface IIdentifierGenerator. You can specifiy the implementing class as generator class for your id and everything works fine.

Only thing you have to take in account, is that hibernate generates the id on session.Save() and not when the object is really flushed to the database, but since you have to assign your id before that at the moment, it shouldn't be a problem.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 13, 2008 5:53 am 
Beginner
Beginner

Joined: Fri Aug 10, 2007 3:34 am
Posts: 44
actually, i generate the id's on a higher level (client), so i have the id's right
before i call a save method on the UI

i managed to avoid that wrong updating when performing insert by
creating an interceptor. My business objects have state info (new, modified
etc) so i can tell the interceptor in the IsUnsaved method when the
objects is unsaved. I also added the version tag at LocationDTO
but that didn't change much....

The problem now is that the update is broken: I'm getting stale exception
at LocationDTO.

thanks once again for the help!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 13, 2008 6:06 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I think you need to specifiy an unsaved value on your version properties or is the default working for you ? Try to specifiy one to see if it changes the behaviour.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 13, 2008 6:32 am 
Beginner
Beginner

Joined: Fri Aug 10, 2007 3:34 am
Posts: 44
I can't use the unsaved-value because my id's are generated before the object is saved.
Unsaved value can take the following values: (documentation)

null - the default for assigned identifiers
any - always performs insert
none - always performs update

Unsaved value doesn't help so i made an interceptor to distinguish between persistent and transient objects.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 13, 2008 6:40 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I meant an usaved value on the version not on the id or do you map the version to the same property ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 13, 2008 7:12 am 
Beginner
Beginner

Joined: Fri Aug 10, 2007 3:34 am
Posts: 44
oh, sorry, my mistake
no, i don't map the version on the same property, i map it on a Ver integer
i generate the Ver with my own version manager.

there was some corrupted data in the database. it could be because of that..


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 13, 2008 7:20 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Try to set your initial version as unsaved-value on the LocationDTO and see if the cascading insert works then. If not, I'm running out of ideas ...

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 14, 2008 5:39 am 
Beginner
Beginner

Joined: Fri Aug 10, 2007 3:34 am
Posts: 44
i resolved the bug with an interceptor. unsaved values couldn't help at all

But now I got hit with another problem. I'll post a topic if I can't resolve it.

Wolli, thanks a lot for the help!


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