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: Many-to-one: 2 questions
PostPosted: Wed Sep 05, 2007 8:23 am 
Newbie

Joined: Wed Apr 04, 2007 9:40 am
Posts: 12
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:1.0.2

Mapping documents:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Competition.Business2.Forums.Post, Competition.Business2" table="t_forum_posts">
<id name="ID" column="PostID" type="int" unsaved-value="0">
<generator class="native" />
</id>

<property name="AddedDate" not-null="false" />
<many-to-one name="AddedBy" column="AddedBy"/>
<property name="AddedByIP" not-null="false" />
<many-to-one name="Forum" column="ForumID" not-null="false"/>
<many-to-one name="ParentPost" column="ParentPostID" not-null="false"/>
<property name="Title" not-null="false" />
<property name="Body" not-null="false" />
<property name="Approved" not-null="false" />
<property name="Closed" not-null="false" />
<property name="ViewCount" not-null="false" />
<property name="ReplyCount" not-null="false" />
<many-to-one name="LastPostBy" column="LastPostBy" not-null="false"/>
<property name="LastPostDate" not-null="false" />

</class>
</hibernate-mapping>


Good day everyone,

I've been using NHib for a little while now and I've always played around an issue I've been having since the beginning for which I cannot find a solution in the documentation. It is probably in there somewhere and my apologies if it is obvious, but I'm writing to the forum as a last resort.

So, first problem: in the previous mapping I want to make the LastPostBy and ParentPost property optional (namely nullable). The database schema allow for nulls in those fields. How do I do that? I thought that not-null would do the trick, but not. When saving the object I get a 'Value cannot be null' error (and the only null parameter is LastPostBy).

Second question: can a many-to-one parameter can be made to load lazily?

Thanks in advance for enlighting me,
Alain-Daniel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 05, 2007 11:16 am 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
The not-null attribute only affects the DDL generated by hbm2ddl, not any NH behavior. Something else seems to be going on here, since <many-to-one> should be able to handle null references automatically--it simply sets the FK to NULL, no problem. I've implemented a simple tree like that before, so I know the ParentPost should be working.

Maybe the issue is with LastPostBy. What is the mapping/class for that?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 05, 2007 12:52 pm 
Newbie

Joined: Wed Apr 04, 2007 9:40 am
Posts: 12
Thank you for your help marcal.

I made a mistake with the database schema and the problem was indeed with LastPostBy and I've fix the problem.

I would still like to know if I can make a many-to-one load lazily, if there are any taker to answer it :-)

Alain-Daniel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 10, 2007 3:48 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
No, but the object itself should load lazily unless it is marked otherwise.

Which is to say: rather than hydrating an object when the many-to-one property is called, a proxy object will be referenced by the property, and the data will be retreived when a property on the proxy is called.


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.