-->
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.  [ 5 posts ] 
Author Message
 Post subject: persisting one-to-one relation
PostPosted: Fri Jan 25, 2008 12:01 pm 
Newbie

Joined: Sun Dec 30, 2007 12:51 pm
Posts: 7
I have such relations :

class List
{
int ID;
Active active
}

class Active
{
ID
Parent list;
}

Two tables :
List:
ID : identity

Active
ID : Foreign key to ID

Mapping :

<class name="List" table="`list`">
<id name="ID" column="`ID`">
<generator class="native" />
</id>
<one-to-one name="Active" class="Active" cascade="all"/>
</class>

<class name="Active" table="`Active`">
<id name="ID" column="`ID`">
<generator class="foreign">
<param name="property">Parent</param>
</generator>
</id>

<one-to-one name="Parent"
class="List" constrainted="true"/>
</class>

Then :
List lst = new List();
Active ac = new Active();

lst.active = ac;
ac.Parent = lst;

Session.save(lst);

The List is persistet correctly, but Active record is not persistet at all.
I have found similar issues in archive of this list but no correct answer for that.

How do I make active record to be persisted automatically in database ?


The problem is that Active object is stored in the nhibernate cache, but not in database.

Here are some logs (List is actually ShoppingList in log, and Active is ActiveShoppingList)

16:55:39.890 [9] DEBUG NHibernate.Transaction.AdoTransaction - begin
16:55:39.890 [9] DEBUG NHibernate.Connection.DriverConnectionProvider - Obtaining IDbConnection from Driver
16:55:40.468 [9] DEBUG NHibernate.Engine.Cascades - unsaved-value: 0
16:55:40.468 [9] DEBUG NHibernate.Impl.SessionImpl - SaveOrUpdate() unsaved instance
16:55:40.484 [9] DEBUG NHibernate.Impl.SessionImpl - saving [Listonic.BLL.Common.Domain.ShoppingList#<null>]
16:55:40.500 [9] DEBUG NHibernate.Engine.Cascades - processing cascades for: Listonic.BLL.Common.Domain.ShoppingList
16:55:40.500 [9] DEBUG NHibernate.Engine.Cascades - done processing cascades for: Listonic.BLL.Common.Domain.ShoppingList
16:55:40.500 [9] DEBUG NHibernate.Impl.SessionImpl - executing insertions
16:55:40.546 [9] DEBUG NHibernate.Impl.WrapVisitor - Wrapped collection in role: Listonic.BLL.Common.Domain.ShoppingList.Items
16:55:40.546 [9] DEBUG NHibernate.Engine.Cascades - unsaved-value: 0
16:55:40.562 [9] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister - Inserting entity: Listonic.BLL.Common.Domain.ShoppingList (native id)
16:55:40.578 [9] DEBUG NHibernate.Impl.BatcherImpl - Opened new IDbCommand, open IDbCommands: 1
16:55:40.578 [9] DEBUG NHibernate.Impl.BatcherImpl - Building an IDbCommand object for the SqlString: INSERT INTO `shoppinglist` (Name, CreationDate, Username) VALUES (?, ?, ?)
16:55:40.578 [9] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister - Dehydrating entity: [Listonic.BLL.Common.Domain.ShoppingList#<null>]
16:55:40.593 [9] DEBUG NHibernate.Type.StringType - binding 'Aa' to parameter: 0
16:55:40.593 [9] DEBUG NHibernate.Type.DateTimeType - binding '2008-01-25' to parameter: 1
16:55:40.593 [9] DEBUG NHibernate.Type.StringType - binding 'aa' to parameter: 2
16:55:40.593 [9] DEBUG NHibernate.SQL - INSERT INTO `shoppinglist` (Name, CreationDate, Username) VALUES (?p0, ?p1, ?p2); ?p0 = 'Aa', ?p1 = '2008-01-25 16:55:38', ?p2 = 'aa'
16:55:40.656 [9] DEBUG NHibernate.Impl.BatcherImpl - Opened new IDbCommand, open IDbCommands: 2
16:55:40.656 [9] DEBUG NHibernate.Impl.BatcherImpl - Building an IDbCommand object for the SqlString: SELECT LAST_INSERT_ID()
16:55:40.656 [9] DEBUG NHibernate.SQL - SELECT LAST_INSERT_ID()
16:55:40.656 [9] DEBUG NHibernate.Impl.BatcherImpl - Opened IDataReader, open IDataReaders: 1
16:55:40.656 [9] DEBUG NHibernate.Type.UInt64Type - returning '20' as column: LAST_INSERT_ID()
16:55:40.656 [9] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister - Natively generated identity: 20
16:55:40.656 [9] DEBUG NHibernate.Driver.NHybridDataReader - running NHybridDataReader.Dispose()
16:55:40.656 [9] DEBUG NHibernate.Impl.BatcherImpl - Closed IDataReader, open IDataReaders :0
16:55:40.656 [9] DEBUG NHibernate.Impl.BatcherImpl - Closed IDbCommand, open IDbCommands: 1
16:55:40.656 [9] DEBUG NHibernate.Impl.BatcherImpl - Closed IDbCommand, open IDbCommands: 0
16:55:40.671 [9] DEBUG NHibernate.Engine.Cascades - processing cascades for: Listonic.BLL.Common.Domain.ShoppingList
16:55:40.687 [9] DEBUG NHibernate.Engine.Cascades - cascading to collection: Listonic.BLL.Common.Domain.ShoppingList.Items
16:55:40.687 [9] DEBUG NHibernate.Engine.Cascades - cascading to SaveOrUpdate()
16:55:40.687 [9] DEBUG NHibernate.Engine.Cascades - unsaved-value: 0
16:55:40.687 [9] DEBUG NHibernate.Impl.SessionImpl - SaveOrUpdate() previously saved instance with id: 20
16:55:40.687 [9] DEBUG NHibernate.Impl.SessionImpl - updating [Listonic.BLL.Common.Domain.ActiveList#20]
16:55:40.687 [9] DEBUG NHibernate.Engine.Cascades - done processing cascades for: Listonic.BLL.Common.Domain.ShoppingList
16:55:40.703 [9] DEBUG NHibernate.Transaction.AdoTransaction - commit
16:55:40.703 [9] DEBUG NHibernate.Impl.SessionImpl - flushing session
16:55:40.718 [9] DEBUG NHibernate.Engine.Cascades - processing cascades for: Listonic.BLL.Common.Domain.ShoppingList
16:55:40.718 [9] DEBUG NHibernate.Engine.Cascades - cascading to collection: Listonic.BLL.Common.Domain.ShoppingList.Items
16:55:40.718 [9] DEBUG NHibernate.Engine.Cascades - cascading to SaveOrUpdate()
16:55:40.718 [9] DEBUG NHibernate.Impl.SessionImpl - SaveOrUpdate() persistent instance
16:55:40.718 [9] DEBUG NHibernate.Engine.Cascades - done processing cascades for: Listonic.BLL.Common.Domain.ShoppingList
16:55:40.718 [9] DEBUG NHibernate.Impl.SessionImpl - Flushing entities and processing referenced collections
16:55:40.734 [9] DEBUG NHibernate.Impl.AbstractVisitor - Processing collection for role Listonic.BLL.Common.Domain.ShoppingList.Items
16:55:40.734 [9] DEBUG NHibernate.Impl.SessionImpl - Collection found: [Listonic.BLL.Common.Domain.ShoppingList.Items#20], was: [<unreferenced>]
16:55:40.750 [9] DEBUG NHibernate.Impl.SessionImpl - Updating entity: [Listonic.BLL.Common.Domain.ActiveList#20]
16:55:40.750 [9] DEBUG NHibernate.Impl.SessionImpl - Processing unreferenced collections
16:55:40.750 [9] DEBUG NHibernate.Impl.SessionImpl - scheduling collection removes/(re)creates/updates
16:55:40.750 [9] DEBUG NHibernate.Impl.SessionImpl - Flushed: 0 insertions, 1 updates, 0 deletions to 2 objects
16:55:40.750 [9] DEBUG NHibernate.Impl.SessionImpl - Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections
16:55:40.750 [9] DEBUG NHibernate.Impl.Printer - listing entities:
16:55:40.765 [9] DEBUG NHibernate.Impl.Printer - Listonic.BLL.Common.Domain.ShoppingList{Active=ActiveList#20, Name=Aa, Username=aa, Items=[], ID=20, CreationDate=2008-01-25, UpdateDate=0001-01-01}
16:55:40.765 [9] DEBUG NHibernate.Impl.Printer - Listonic.BLL.Common.Domain.ActiveList{Parent=ShoppingList#20, ListId=20}
16:55:40.765 [9] DEBUG NHibernate.Impl.SessionImpl - executing flush
16:55:40.765 [9] DEBUG NHibernate.Impl.ConnectionManager - registering flush begin
16:55:40.781 [9] DEBUG NHibernate.Impl.ConnectionManager - registering flush end
16:55:40.781 [9] DEBUG NHibernate.Impl.SessionImpl - post flush
16:55:40.781 [9] DEBUG NHibernate.Impl.SessionImpl - before transaction completion
16:55:40.843 [9] DEBUG NHibernate.Impl.ConnectionManager - aggressively releasing database connection
16:55:40.843 [9] DEBUG NHibernate.Connection.ConnectionProvider - Closing connection
16:55:40.859 [9] DEBUG NHibernate.Impl.SessionImpl - transaction completion
16:55:40.859 [9] DEBUG NHibernate.Transaction.AdoTransaction - running AdoTransaction.Dispose()


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 25, 2008 12:32 pm 
Regular
Regular

Joined: Wed Jun 21, 2006 3:13 pm
Posts: 110
Drak,

I had a similar problem in the past with a one-to-one relationship. In my case, the issue was that, because the id wasn't zero, NHibernate didn't know to do an insert.

As a quick test, you might want to do the following with Active:

<id name="ID" column="`ID`" unsaved-value="any">
<generator class="foreign">
<param name="property">Parent</param>
</generator>
</id>

This will tell NHibernate to issue a Save no matter what (note, you don't want to leave things in this state as it will attempt to do an insert always)

For more information, check out 17.4. Using cascading Update() in the NHibernate documentation.

In my case, the easiest solution was to add an int column to my assigned id table (the child in the one-to-one) and map it to a version attribute in NHibernate:

<version name="Version" column="Version" unsaved-value="0" />

When NHibernate sees the version column, it can inspect the value (0 for a new class instance) to determine if it should insert an update or an insert. Everything else will be transparent to you except for adding this column.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 25, 2008 1:42 pm 
Newbie

Joined: Sun Dec 30, 2007 12:51 pm
Posts: 7
This is a correct track. However i have a problem with your solution.

I Tried to use timestamp tag but got an exception

The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'timestamp' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'urn:nhibernate-mapping-2.2:property urn:nhibernate-mapping-2.2:many-to-one urn:nhibernate-mapping-2.2:one-to-one urn:nhibernate-mapping-2.2:component urn:nhibernate-mapping-2.2:dynamic-component urn:nhibernate-mapping-2.2:any urn:nhibernate-mapping-2.2:map urn:nhibernate-mapping-2.2:set urn:nhibernate-mapping-2.2:list urn:nhibernate-mapping-2.2:bag urn:nhibernate-mapping-2.2:idbag urn:nhibernate-mapping-2.2:array urn:nhibernate-mapping-2.2:primitive-array urn:nhibernate-mapping-2.2:subclass urn:nhibernate-mapping-2.2:joined-subclass urn:nhibernate-mapping-2.2:loader urn:nhibernate-mapping-2.2:sql-insert urn:nhibernate-mapping-2.2:sql-update urn:nhibernate-mapping-2.2:sql-delete urn:nhibernate-mapping-2.2:filter'."

Same happens for version tag...

Can you help me with that ?
Tried to find solution for that but no luck...

Im using version 1.2.1.GA of Nhibernate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 25, 2008 2:20 pm 
Regular
Regular

Joined: Wed Jun 21, 2006 3:13 pm
Posts: 110
Can you provide an updated mapping file?

Here is mine:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
   <class name="PersonRage.Person, PersonRage" table="Person">
      <id name="Id" type="Int32" access="nosetter.camelcase" >
         <column name="personId" not-null="true" unique="true" />
         <generator class="native" />
      </id>

      <version name="Version" column="Version" unsaved-value="0" access="nosetter.camelcase"/>
      
      <property name="Name" column="Name" />
      <property name="Status" column="Status" type="PersonRage.Person+PersonStatus, PersonRage" />
      
   </class>
</hibernate-mapping>


Note! The Version element must come before any Property elements.

If you are using Visual Studio to write your code and mappings, then you should specify the NHibernate XSDs for your XML mapping files... it will give you intellisense and cut down on errors. Simply open up your mapping file and go to the properties window. Click the schema property and navigate to your nhibernate source directory (get the code and unzip it if you don't have it already). Select the mapping and generic XSDs.

My schema property for the above xml file looks like:

"C:\Program Files\NHibernate\src\src\NHibernate\nhibernate-generic.xsd" "C:\Program Files\NHibernate\src\src\NHibernate\nhibernate-mapping.xsd"

This will help you catch and order errors in defining elements.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 25, 2008 4:22 pm 
Newbie

Joined: Sun Dec 30, 2007 12:51 pm
Posts: 7
Thank you. Putting Timestamp field before any properties totally resolved my problem.

Once again your advice turned out to be very helpful.
Thank you very much :-)


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