-->
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.  [ 2 posts ] 
Author Message
 Post subject: Updating using collections
PostPosted: Tue Jul 15, 2008 4:13 pm 
Newbie

Joined: Tue Jul 15, 2008 3:55 pm
Posts: 5
This is jus Sample which resembles with my actual tables..

Table A
f1 int
f2 int

Table B
BF1 int
BF2 int ( References to Table A f1)

Entitys:
public class EntityTableA
{
private int _f1;
private IList<EntityTableB> _BF1=new list<EntityTableB>();

public virtual int f1
{
..etc
}

public virtual IList<EntityTableB> BF2
{
...etc
}

}

public class EntityTableB
{

private int _BF1
private init _BF2;

public virutal int BF1
{
..etc
}

public virtual int BF2
{
...etc
}
}
}


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

<class name="EntityTableA" table="TableA" >
<id name="f1" column="f1" type="System.Int32" unsaved-value="0">
<generator class="identity" />
</id>

<bag name="coll_f2" cascade="all" lazy="false">
<key column="f1"/>
<one-to-many class="EntityTableA"/>
</bag>
</class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="BF1" table="TableB" >
<id name="BF1" column="BF1" type="Int32" unsaved-value="0">
<generator class="identity" />
</id>
<property name="BF2" column="BF2" type="Int32" length="4" />
</class>
</hibernate-mapping>

Question:
When i load "EntityTableA" am getting the data and it is working fine but when i update collection(EntityTableB) using following code it is throwing error.

ICriteria criteria = _session.CreateCriteria(typeof(EntityTableA));
criteria.Add(NHibernate.Expression.Expression.Eq("f1", 1));
EntityTableA dPop = new EntityTableA();
dPop = (EntityTableA)criteria.List()[0];
dPop.EntityTableB = null; // -- Here am making Table B entity as null
_session.SaveOrUpdate(dPop);


please help me..


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 15, 2008 4:48 pm 
Newbie

Joined: Wed Sep 06, 2006 8:33 am
Posts: 19
Hi...
The problem is in your EntityTableB... you use the ID from EntityTableA it's wrong... you have to use EntityTableA... see:

Quote:
public class EntityTableA
{
private int _f1;
private IList<EntityTableB> _BF1=new list<EntityTableB>();

public virtual int f1
{
..etc
}

public virtual IList<EntityTableB> BF2
{
...etc
}

}

public class EntityTableB
{

private int _BF1
private EntityTableA _BF2;

public virutal int BF1
{
..etc
}

public virtual EntityTableA BF2
{
...etc
}
}
}


now, the mapping:

Quote:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

<class name="EntityTableA" table="TableA" >
<id name="f1" column="f1" type="System.Int32" unsaved-value="0">
<generator class="identity" />
</id>

<bag name="BF2" table="TableB" cascade="all" lazy="false">
<key column="f1"/>
<one-to-many class="EntityTableA"/>
</bag>
</class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="BF1" table="TableB" >
<id name="BF1" column="BF1" type="Int32" unsaved-value="0">
<generator class="identity" />
</id>
<many-to-one name="BF2" class="EntityTableA" column="f1" />
</class>
</hibernate-mapping>


I think this solve your problem.


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