Hibernate Books

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: help needed in mapping collection
PostPosted: Mon Jan 28, 2008 8:25 am 
Beginner
Beginner

Joined: Wed Aug 29, 2007 8:23 am
Posts: 31
Hello.

I'm trying to build a one way navigation graph but I'm having some problems with the persistence of an associated collection. Here's my scenario (simplified for easier understanding):

public class Factura{
Int32 _id;
Int32 _version;

//other simple properties
IList<ItemFactura> _entradas;
}

public class ItemFactura
{
Int32 _id;
Int32 _version;
//more properties and a collection of components
//but it's not important here so I've removed it
}

now, I've got something like this on my hbm files:

Factura.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping
xmlns="urn:nhibernate-mapping-2.2"
schema="dbo"
assembly="SRA.Mercados.Facturacao"
namespace="SRA.Mercados.Facturacao">
<class name="Factura"
table="Factura" lazy="false">
<id name="Id" column="IdFactura" unsaved-value="0">
<generator class="identity" />
</id>
<version name="Version" column="Version"
type="int"/>

<!-- other properties removed -->

<bag name="_entradas" access="field" lazy="false" cascade="all">
<key column="IdFactura" />
<one-to-many class="ItemFactura"/>
</bag>
</class>
</hibernate-mapping>

and the itemfactura.hbm looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping
xmlns="urn:nhibernate-mapping-2.2"
schema="dbo"
assembly="SRA.Mercados.Facturacao"
namespace="SRA.Mercados.Facturacao">
<class name="ItemFactura"
table="ItemFactura" lazy="false">
<id name="Id" column="IdItemFactura" unsaved-value="0">
<generator class="identity" />
</id>
<version name="Version" column="Version"
type="int"/>
<property name="Desconto" column="Descontos" />

<!--other properties removed -->

</class>
</hibernate-mapping>

Now the question: to propagate an insert do I need to make my relationship a two way relationship? My current problem is that when nhibernate propagates the insertion it won't use the <key> element defined inside the <bag> element of the factura mapping element.

I mean, when I try to insert a factura on the db, NH will generate this SQL:

2008-01-28 12:14:01,271 [8] DEBUG NHibernate.SQL - INSERT INTO dbo.ItemFactura (Version, Descontos) VALUES (@p0, @p1); select SCOPE_IDENTITY(); @p0 = '1', @p1 = '0'

The problem is that it won't insert the IdFactura column when generating the sql for inserting the itemfactura entry on the table.

so, what am I doing wrong?

thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 28, 2008 5:23 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
In a unidirectional one-to-many, NH first inserts the rows on the many end without the FK, then updates to include it. This creates issues if the FK column is not null. The usual remedy is to make the relationship fully bidirectional, and invert the collection. This way the FK is handled by the child, and is cleanly inserted.

Short answer: yes :)

If you don't want to maintain these semantics, you could map Factura as a component-element, which I tend to prefer to a uni-directional one-to-many.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 28, 2008 5:27 pm 
Beginner
Beginner

Joined: Wed Aug 29, 2007 8:23 am
Posts: 31
Hello.

hum...I see...I've ended up reverting the direction since I cannot change the db and the FK field is configured so that it doesn't accept nulls.

The reason I didn't follow your advice is that ItemFactura has already a collection of components and I recall having seen something in the docs which say that this is not recommended (maybe I'm wrong).

thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 29, 2008 12:13 am 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
First: sorry, I meant <composite-element>, not <component-element>.

Second: you are right, full entities are encouraged for anything that might have its own components, required for any object that will have it's own collections. You simplified your model for the question to where <composite-element> looked like an attractive option, but it might just not be possible in the your full model.

Good luck!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 29, 2008 4:29 am 
Beginner
Beginner

Joined: Wed Aug 29, 2007 8:23 am
Posts: 31
Hello again.

yeah, you're right. btw, supposing that I didn't want to transform my model so that itemfactura got promoted to an entity, how would I solve it? I mean, I didn't run the code, but I'm thinking that my initial approach would be able to load the object graph from the db, right? so maybe I would only have to write SQL for the insert/update/delete scenarios. Am I wrong?

thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 30, 2008 2:09 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
I'm not sure what you mean by "not" promoting to an entity, since it already is (it has it's own <class> mapping).

At any rate, yes, the only hole in your situation is in the case of inserts (maybe deletes, too) because of the non-null FK reference. Querying should work just fine if you wanted to use another route for inserting.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 30, 2008 3:12 pm 
Beginner
Beginner

Joined: Wed Aug 29, 2007 8:23 am
Posts: 31
hello again marcal.

well, i was asking if I couldn't change my model so that i had a i had something like

root entity-> collection components ->collection components

would my initial mapping work in this scenario? (ie, having a unidirectional relationship)?


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