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.  [ 3 posts ] 
Author Message
 Post subject: To control Pattern Parent/Child:
PostPosted: Tue Sep 11, 2007 10:56 am 
Newbie

Joined: Thu Jul 12, 2007 1:35 pm
Posts: 9
Salamoalikome, Hello, I use in my application Pattern Parent/Child, and I encountered problems:
The code:
The classes persistences :
public class Client
{
private String Nom;
public virtual String CltNom
{
get
{
return Nom;
}
set
{
Nom = value;
}
}

private Int32 Version;
public virtual Int32 CltVersion
{
get
{
return Version;
}
set
{
Version = value;
}
}

private IList listePersons;
public virtual IList CltListePersons
{
get
{
return listePersons;
}
set
{
listePersons = value;
}
}
}

public class Person
{
private String Nom;
public virtual String PerNom
{
get
{
return Nom;
}
set
{
Nom = value;
}
}

private Nullable<DateTime> Version;
public virtual Nullable<DateTime> PerVersion
{
get
{
return Version;
}
set
{
Version = value;
}
}

private Client client = new Client();
public virtual Client PerClient
{
get
{
return client;
}
set
{
client = value;
}
}

private IList<ProductType> _productTypes;
public virtual IList<ProductType> productTypes
{
get
{
return _productTypes;
}
set
{
_productTypes = value;
}
}
}

public class ProductType
{
private int ProductTypeID;
public virtual int productTypeID
{
get
{
return ProductTypeID;
}
set
{
ProductTypeID = value;
}
}

private string ProductTypeName;
public virtual string productTypeName
{
get
{
return ProductTypeName;
}
set
{
ProductTypeName = value;
}
}
private Nullable<DateTime> _version;
public virtual Nullable<DateTime> version
{
get
{
return _version;
}
set
{
_version = value;
}
}

private Person _person = new Person();
public virtual Person person
{
get
{
return _person;
}
set
{
_person = value;
}
}
}

The files mapping:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="TestList">
<class name="TestList.Client, TestList" table="Client">

<id name="CltNom" column="Nom" type="String" >
<generator class="assigned"/>
</id>

<version name="CltVersion" column="Version" generated="never" unsaved-value="negative"/>

<bag name="CltListePersons" cascade="all-delete-orphan" inverse="true">
<key column="CltID"/>
<one-to-many class="TestList.Person, TestList"/>
</bag>

</class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="TestList">
<class name="TestList.Person, TestList" table="Person">

<id name="PerNom" column="Nom" type="String" >
<generator class="assigned"/>
</id>

<!--<version name="PerVersion" column="Version" generated="never" unsaved-value="negative" />-->
<timestamp name="PerVersion" column="Version" generated="never"/>

<many-to-one name="PerClient" class="TestList.Client, TestList" column="CltID"/>

<bag name="productTypes" cascade="all">
<key column="PersonNom"/>
<one-to-many class="TestList.ProductType, TestList"/>
</bag>
</class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="TestList">
<class name="TestList.ProductType, TestList" table="ProductType" lazy="true" mutable="true">

<id name="productTypeID" column="ProductTypeID" type="int" >
<generator class="assigned"/>
</id>

<!--<version name="version" column="Version" generated="never" unsaved-value="negative"/>-->
<timestamp name="version" column="Version" generated="never"/>
<property name="productTypeName" column="ProductTypeName" type="String"/>

<many-to-one name="person" class="TestList.Person, TestList" column="PersonNom"/>
</class>
</hibernate-mapping>

When I try to add Child (ProductType) the program generates the following error:
object references an unsaved transient instance - save the transient instance before flushing: TestList.Person.

Thanks for helping me well to find a solution!!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 11, 2007 1:50 pm 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hello,

Are you trying to save the ProductType before the Person actually got saved ? NHibernate won't do that automatically.

If you add the ProductType to the Person.ProductTypes collection and then you execute SaveOrUpdate(person), it should work. But if you assign ProductType.Person = person (where person is transient), you'll get this error.

Otherwise, could you post the code that actually executes the save ?

_________________
Please rate this post if it helped.

X.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 11, 2007 2:44 pm 
Newbie

Joined: Thu Jul 12, 2007 1:35 pm
Posts: 9
My interest is to minimize the code and it is that the utility of Pattern Parent/Child, in any case thank you for your assistance!!!


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