-->
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: Newbie - One to one mapping
PostPosted: Thu Jan 05, 2006 7:10 pm 
Newbie

Joined: Thu Jan 05, 2006 5:57 pm
Posts: 1
Hello,

I'm pretty new to NHibernate and there is a thing I cannot solve.

In my project I have an Object called 'Factuurregel' which contains a reference to an Object 'Artikel'.
My project can have many Factuurregels, but each Factuurregel can only have 1 reference to an 'Artikel'.

As long as try to persist 'simple' things such as the name, the data goes perfectly in my database.

Now, I would like to be able to save my Factuurregel so that the Artikel it contains is also automatically saved. Now, this is basic one-to-one relation ship. A Factuurregel contains 1 Artikel.

=========================================================

Public Class FactuurRegel
Private m_ID As Int32
Private m_Aantal As Integer
Private m_Artikel As Artikel
Private m_AangerekendeStukPrijs As Double
Private m_PrijsExlusiefBTW As Double
Public Property PrijsExlusiefBTW() As Double

Get
Return m_PrijsExlusiefBTW
End Get
Set(ByVal Value As Double)
m_PrijsExlusiefBTW = Value
End Set
End Property



Public Property AangerekendeStukPrijs() As Double
Get
Return m_AangerekendeStukPrijs
End Get
Set(ByVal Value As Double)
m_AangerekendeStukPrijs = Value
End Set
End Property



Public Property Aantal() As Integer
Set(ByVal Value As Integer)
m_Aantal = Value
End Set
Get
Return m_Aantal
End Get
End Property

Public ReadOnly Property Artikel() As Artikel
Get
Return m_Artikel
End Get
End Property

=====================================================
Public Class Artikel
Private m_ID As Int32
Private m_Verkoopprijs As Single
Private m_Omschrijving As String
Private m_btwpercentage As Byte
Private m_Groep As String

Public ReadOnly Property Verkoopprijs() As Single
Get
Return m_Verkoopprijs
End Get
End Property

Public ReadOnly Property Omschrijving() As String
Get
Return m_Omschrijving
End Get
End Property

Public ReadOnly Property Groep() As String
Get
Return m_Groep
End Get
End Property

Public ReadOnly Property Btwpercentage() As Byte
Get
Return m_btwpercentage
End Get
End Property
=======================================================
Mapping files
=======================================================

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Paperclip.FactuurRegel, Paperclip" table="factuurregel">
<id name="m_ID" access="field" column="FactuurRegelID" type="Int32">
<generator class="native" />
</id>


<property name="Aantal" type="Int32"/>
<one-to-one name="Paperclip.Artikel, Paperclip" class="Paperclip.Artikel, Paperclip" column="ArtikelID" not-null="true" cascade="delete"/>




</class>
</hibernate-mapping>

=======================================================

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Paperclip.Artikel, Paperclip" table="artikels">
<id name="m_ID" access="field" column="ArtikelID" type="Int32">
<generator class="native" />
</id>
<property name="Verkoopprijs" type="Double"/>
<property name="Omschrijving" type="String" />
<property name="Groep" type="String"/>
<property name="Btwpercentage" type="Byte"/>


<set name="FactuurRegel" >
<key column="ArtikelID" />
<one-to-one class="Paperclip.FactuurRegel, Paperclip" />
</set>


</class>
</hibernate-mapping>

========================================================

The more I check examples and read information, the more I get confused.

All I want to do is to save the Object 'Factuurregel' and it should automatically add the 'artikel' it contains in an other table.

All I need to save from Factuurregel is "Aantal" all the other fields can be calculated.

In my opinion, the Artikel should have no knowledge of the 'Factuurregel' it belongs to whatsoever...

It's probably something very simpel which I overlooked, but after days of search I'm totally desperate. It could also be that my theory is wrong...

Thanks in advance for reading and (hopefully) helping me out....


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 05, 2006 7:51 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
If Artikel is only referenced by FactuurRegel, you should define Artikel's generator class as "foreign", and add constrained="true" to it's one-to-one definition. I seem to recall that this is required for cascading to work.

Also, in the mapping of FactuurRegel, you want your one-to-one property's name attribute to be just "Artikel", not the assembly-qualified type name "ParperClip.Artikel, Paperclip". The fully qualified class name is in the class attribute, which you already have.


Top
 Profile  
 
 Post subject: I have the same issue
PostPosted: Thu Jan 05, 2006 9:57 pm 
Newbie

Joined: Thu Jan 05, 2006 7:33 pm
Posts: 13
From what I can tell, there are 2 options for a one-to-one relationship. The TAG "one-to-one" is for creating relationships on primary keys - so CLASS A and CLASS B, in different tables, have matching primary keys. The trick here is that the keys must match when generated so, as noted above, you need to use the FOREIGN generator class.

http://www.hibernate.org/hib_docs/v3/re ... n-onetoone

The other option is a simple <many-to-one> TAG that has unique="true" set. Im trying this but unless I save all children manually, I get foreign key constraint errors!


Top
 Profile  
 
 Post subject: finally worked out that it was the "unseaved-value"
PostPosted: Thu Jan 05, 2006 10:18 pm 
Newbie

Joined: Thu Jan 05, 2006 7:33 pm
Posts: 13
I had set UNSAVED-VALUE="null" in the <id> tag of the child class. Since the id property of the java class was a long it wont be null and therefore hibernate was assuming it was a detached instance.

According to the docs, the UNSAVED-VALUE tag is hardly needed in hibernate 3

http://www.hibernate.org/hib_docs/v3/re ... aration-id

Quote:
If the name attribute is missing, it is assumed that the class has no identifier property.

The unsaved-value attribute is almost never needed in Hibernate3.

There is an alternative <composite-id> declaration to allow access to legacy data with composite keys. We strongly discourage its use for anything else.


md


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.