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: Help about relations...
PostPosted: Tue Dec 02, 2008 12:49 pm 
Newbie

Joined: Mon Nov 17, 2008 6:42 am
Posts: 6
Dear nH users,
I'm a beginner of nHibernate and I have a question about releation.
I have a main object called Contenzioso and it has some details with a number and a dateTime.

It works great, but if I use this code, the DateNumberDetails previus store into database will not delete automatically...

[...]
Contenzioso c = ..."session.get(id)";...
c.eventA = null;

Probably I miss something in the DateNumberDetails mapping file...
The strange things is that if I assign a new DateNumberDetails with new values, nHibernate replace the values of the old row into the db.


the POCO classes are like

class Contenzioso
{
public long id {get;}
public string description {get;set;}
public DateNumberDetails eventA {get;set;}
public DateNumberDetails eventB {get;set;}
}


class DateNumberDetails
{
public long id {get;}
public DateTime date {get;set;}
public string number {get;set;}
}




Hibernate version: 2.0.1

Mapping documents:
is something like this...

<class name="Contenzioso" table="dbo.Contenziosi" lazy="true">

<id name="id" unsaved-value="0" access="field" type="System.Int64">
<column name="id"/>
<generator class="native" />
</id>

<property name="description" column="description" type="System.String" />

<many-to-one name="eventA "
class="DateNumberDetails"
column="eventAId"
cascade="all-delete-orphan"
/>


<many-to-one name="eventB "
class="DateNumberDetails"
column="eventAId"
cascade="all-delete-orphan"
/>
[...]
</class>

<class name="DateNumberDetails" table="dbo.DateNumberDetails" lazy="true">

<id name="id" unsaved-value="0" access="field" type="System.Int64">
<column name="id"/>
<generator class="native" />
</id>

<property name="Data" column="Data" type="System.DateTime" />
<property name="Numero" column="Numero" type="System.String" />

</class>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2008 5:14 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Quote:
Contenzioso c = ..."session.get(id)";...
c.eventA = null;

Probably I miss something in the DateNumberDetails mapping file...
The strange things is that if I assign a new DateNumberDetails with new values, nHibernate replace the values of the old row into the db.


Cascade only works if you explicitly delete c. Setting eventA to null is something different. And when you speak about setting new values, did the new detail had an id = 0 ?

Your "detail" sounds more like a one-to-one association anyway. Is there a reason for a many-to-one ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2008 12:07 pm 
Newbie

Joined: Mon Nov 17, 2008 6:42 am
Posts: 6
wolli wrote:
Quote:
And when you speak about setting new values, did the new detail had an id = 0 ?

Your "detail" sounds more like a one-to-one association anyway. Is there a reason for a many-to-one ?


I will check the id value.

I read the reference and some manual and seems that the one-to-one is achieved using the same id for both of the row in the two tables... Each property is one-to-one, but the Contenzioso row has more details...
Probably the right solution is one-to-one... but how I can do that using explicit id into Contenzioso table?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2008 12:53 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
If one row can have more details than you need a one-to-many. Which then will be a list of details on the Contenzioso class.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 04, 2008 5:05 am 
Newbie

Joined: Mon Nov 17, 2008 6:42 am
Posts: 6
wolli wrote:
If one row can have more details than you need a one-to-many. Which then will be a list of details on the Contenzioso class.


Not exactly a list of details, becuase every detail has a different semantic and is not only a set.
For example a detail is the initial information (date and number of protocol), another is the validation information and so on...
Not all the details are available for all contenzioso.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 04, 2008 5:15 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can try unique="true" on the many-to-one. Maybe that will get you the expected deletes. If you move to a one-to-one you can use property-ref to use properties different than the id on the target end.

_________________
--Wolfgang


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.