-->
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.  [ 9 posts ] 
Author Message
 Post subject: Simple Collection mapping
PostPosted: Thu Mar 19, 2009 10:27 pm 
Newbie

Joined: Thu Mar 19, 2009 10:16 pm
Posts: 5
I seem to be a bit thick as I cant get this to work.

I can get User to work without the collection.


could not insert: [NHiberTest.Objects.Note][SQL: INSERT INTO Notes (Note) VALUES (@p0); select SCOPE_IDENTITY()]

public class User
{
public virtual int ID { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual IList<Note> Notes{ get; set; }

}

public class Note
{
public virtual int ID{get;set;}
public virtual string Text { get; set; }
}

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NHiberTest.Objects.User,NHiberTest.Objects" table="[User]">

<id name="ID" column="ID" unsaved-value ="0" type="System.Int32">
<generator class="identity" />
</id>

<property name="FirstName" column="FirstName"/>
<property name="LastName" column ="LastName"/>


<bag generic="true" name="Notes" table="Notes" cascade="all">
<key column="UserID" not-null="true"/>
<one-to-many class ="NHiberTest.Objects.Note,NHiberTest.Objects"/>
</bag>


</class>


</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NHiberTest.Objects.Note,NHiberTest.Objects" table="Notes">

<id name="ID" column="ID" unsaved-value ="0" type="System.Int32">
<generator class="identity" />
</id>

<property name="Text" column ="Note"/>

</class>


</hibernate-mapping>

ISession s = NHibernateSessionFactory.OpenSession();

ITransaction t = s.BeginTransaction();

User u = new User();
u.LastName="Wightman";
u.FirstName="Mike";


u.Notes = new List<Note>();

Note n=new Note();
n.Text="This is my note";


u.Notes.Add(n);

s.SaveOrUpdate(u);

t.Commit();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 20, 2009 3:17 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You probably have a not-null constraint on UserId in Notes ? When you specifiy the association in this way, hibernate first inserts the note and then updates the UserId. Check out the doc:

http://nhforge.org/doc/nh/en/index.html#example-parentchild

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 20, 2009 6:36 am 
Regular
Regular

Joined: Wed Feb 11, 2009 10:58 am
Posts: 55
maybe just use inverse in the bag?

<bag generic="true" name="Notes" table="Notes" cascade="all" inverse="true">
<key column="UserID" not-null="true"/>
<one-to-many class ="NHiberTest.Objects.Note,NHiberTest.Objects"/>
</bag>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 20, 2009 7:06 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Inverse is just one part of the solution. You need a reference to User on Note.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 20, 2009 2:49 pm 
Newbie

Joined: Thu Mar 19, 2009 10:16 pm
Posts: 5
I was under the impression that I did not need to refrence User from Note mapping unless I wanted the note to hold a back refrence to its owner.



back pointer example.
class
{
string name
Ilist<note> notes
}


class note
{
user owner <-back pointer
text
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 20, 2009 3:06 pm 
Newbie

Joined: Thu Mar 19, 2009 10:16 pm
Posts: 5
http://bchavez.bitarmory.com/archive/20 ... ibute.aspx



In the unidirectional case, the foreign key column in the child table doesn't map to a property in the child object; it is in the data model, but not the object model. Since it is unidirectional there is just a property in the parent object [that contains a collection of children]; not the child [the child object does not contain any information back to the parent]. In addition, the foreign key column [in the child table] has to be defined as nullable because Hibernate will first insert the child row (with a NULL foreign key) and then update it [the inserted row] later [with the parent's primary key].


I found this post explaining what I wish to accomplish and was a bit put put off by the statement "In addition, the foreign key column[in the child table] has to be defined as nullable because Hibernate will first insert the child row". From a DBA perspective this is a huge no-no and in most companies would not be accepted.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 21, 2009 5:51 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
That's the way hibernate works. If you define a uni-directional association, the parent is the owner and does an update on the child.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 21, 2009 9:05 pm 
Newbie

Joined: Thu Mar 19, 2009 10:16 pm
Posts: 5
wolli wrote:
That's the way hibernate works. If you define a uni-directional association, the parent is the owner and does an update on the child.


This is an interesting architectural decision might I as what factors influenced this design.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 22, 2009 8:13 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Can't give you more details on that decission. You might ask the question on the developer list or nhusers group at google.

_________________
--Wolfgang


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