-->
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: Newbie's first one-to-many works, without DB Integ check
PostPosted: Mon Aug 13, 2007 7:27 pm 
Newbie

Joined: Fri Jul 20, 2007 7:45 pm
Posts: 12
Location: Amgen Corp
Hi,

I'm just loving NHibernate. I've got several O/R mappings working thanks to this forum. Today I tried my first "one-to-many" involving two tables. It works perfectly, with one exception. If I define the foreign key of the child table to the database server (MS2005), in order to enforce relational integrity, my ITransaction.Commit() fails with this message:

"The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Claimant_Pet". The conflict occurred in database "RentAssist", table "dbo.Claimant", column 'claimant_id'.
The statement has been terminated."

When I pull out the relation in the database - the application works perfectly, with the correct foreign keys pointing to the correct parent for any number of children (pets) and transactions.

Here's my classes and mapping file (with extraneous properties removed):

public class Claimant
{
private Int32 claimant_id;
private String claimant_name;
private DateTime claimant_bday;
private Decimal claimant_rent;
private Decimal claimant_net_pay;
private DateTime claimant_app_date;
private Boolean claimant_calif;
private Boolean claimant_has_pet;
private Boolean claimant_related;
private ISet claimant_pets;
// The normal public virtual getters and setters are here.

public class Pet
{
private Int32 pet_id;
private String pet_name;
private Int32 claimant_id;
private String pet_species;
private Boolean pet_lives_outside;
private Boolean pet_lives_cage;
private Boolean pet_lives_aquar;
private Boolean pet_has_box;
// The normal public virtual getters and setters are here.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="RentAssist" assembly="RentAssist">
<class name="RentAssist.Claimant" table="Claimant">
<!-- The Claimant_id is the primary key. -->
<id name="Claimant_id" type="Int32" column="claimant_id">
<generator class="hilo">
<param name="table">claimant_hi_value</param>
<param name="column">next_value</param>
<param name="max_lo">0</param>
</generator>
</id>
<!-- A Claimant has a bunch of other properties mapped here -->
<!-- A Claimant can have a collection of pets -->
<set name="Claimant_pets" cascade="all" lazy="true">
<key column="claimant_id"/>
<one-to-many class="Pet"/>
</set>
</class>

<class name="RentAssist.Pet" table="Pet">
<!-- The Pet_id is the primary key. -->
<id name="Pet_id" type="Int32" column="pet_id">
<generator class="hilo">
<param name="table">pet_hi_value</param>
<param name="column">next_value</param>
<param name="max_lo">0</param>
</generator>
</id>
<!-- A Pet has a name, of up to 25 chars -->
<property name="Pet_name">
<column name="pet_name" length="25"/>
</property>
<property name="Pet_species">
<column name="pet_species" length="5"/>
</property>
<property name="Claimant_id" column="claimant_id" type="Int32"/>
<!—A Pet has a bunch of other properties mapped here ->
</class>
</hibernate-mapping>

Anyone see what I'm doing wrong?

_________________
Fred Stann
Solutions Architect


Top
 Profile  
 
 Post subject: Getting Unknown Entity Class now
PostPosted: Tue Aug 14, 2007 1:06 pm 
Newbie

Joined: Fri Jul 20, 2007 7:45 pm
Posts: 12
Location: Amgen Corp
Hi,

My problem is that a cascading one-to-many association works fine, unless I turn on the foreign key integrity check in the DB server. Then NHibernate throws an exception, even though the foreign key it generated is totally valid. If I remove the foreign key constraint in the DB, NHibernate persists the parent and children, and the children have the correct foreign key.

I decided to try and put a many-to-one element in the target class thinking that NHibernate had to "know" about the constraint.

so the parent class (RentAssist.Claimant) now has:

<id name="Claimant_id" type="Int32" column="claimant_id">
<generator class="hilo">
<param name="table">claimant_hi_value</param>
<param name="column">next_value</param>
<param name="max_lo">0</param>
</generator>
</id>
<set name="Claimant_pets" inverse="true" cascade="all-delete-orphan" lazy="true">
<key column="pet_claimant_id"/>
<one-to-many class="Pet"/>
</set>

and the pet class has this:

<id name="Pet_id" type="Int32" column="pet_id">
<generator class="hilo">
<param name="table">pet_hi_value</param>
<param name="column">next_value</param>
<param name="max_lo">0</param>
</generator>
</id>
<property name="Pet_claimant_id" type="Int32" column="pet_claimant_id"/>
<many-to-one name="Pet_claimant_id" class="RentAssist.Claimant" column="claimant_id"/>

Now I get an exception in ISession.save {"Unknown entity class: System.Int32"}

Here's some stack:
NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(Type theClass)\r\n at NHibernate.Impl.SessionImpl.GetClassPersister(Type theClass)\r\n at NHibernate.Impl.SessionImpl.GetEntityPersister(Object obj)\r\n at NHibernate.Impl.SessionImpl.IsUnsaved(Object obj, Boolean earlyInsert, Object self)\r\n at NHibernate.Impl.SessionImpl.NullifyTransientReferences(Object value, IType type, Boolean earlyInsert, Object self)\r\n at NHibernate.Impl.SessionImpl.NullifyTransientReferences(Object[] values, IType[] types, Boolean earlyInsert, Object self)\r\n at NHibernate.Impl.SessionImpl.DoSave(Object theObj, EntityKey key, IEntityPersister persister, Boolean replicate, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)\r\n at NHibernate.Impl.SessionImpl.DoSave(Object obj, Object id, IEntityPersister persister, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)\r\n at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)\r\n at NHibernate.Impl.SessionImpl.Save(Object obj)\r\n at NHibernate.Impl.SessionImpl.SaveOrUpdate(Object obj)\r\n

Any help?

_________________
Fred Stann
Solutions Architect


Top
 Profile  
 
 Post subject: Getting Unknown Entity Class now
PostPosted: Tue Aug 14, 2007 1:06 pm 
Newbie

Joined: Fri Jul 20, 2007 7:45 pm
Posts: 12
Location: Amgen Corp
Hi,

My problem is that a cascading one-to-many association works fine, unless I turn on the foreign key integrity check in the DB server. Then NHibernate throws an exception, even though the foreign key it generated is totally valid. If I remove the foreign key constraint in the DB, NHibernate persists the parent and children, and the children have the correct foreign key.

I decided to try and put a many-to-one element in the target class thinking that NHibernate had to "know" about the constraint.

so the parent class (RentAssist.Claimant) now has:

<id name="Claimant_id" type="Int32" column="claimant_id">
<generator class="hilo">
<param name="table">claimant_hi_value</param>
<param name="column">next_value</param>
<param name="max_lo">0</param>
</generator>
</id>
<set name="Claimant_pets" inverse="true" cascade="all-delete-orphan" lazy="true">
<key column="pet_claimant_id"/>
<one-to-many class="Pet"/>
</set>

and the pet class has this:

<id name="Pet_id" type="Int32" column="pet_id">
<generator class="hilo">
<param name="table">pet_hi_value</param>
<param name="column">next_value</param>
<param name="max_lo">0</param>
</generator>
</id>
<property name="Pet_claimant_id" type="Int32" column="pet_claimant_id"/>
<many-to-one name="Pet_claimant_id" class="RentAssist.Claimant" column="claimant_id"/>

Now I get an exception in ISession.save {"Unknown entity class: System.Int32"}

Here's some stack:
NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(Type theClass)\r\n at NHibernate.Impl.SessionImpl.GetClassPersister(Type theClass)\r\n at NHibernate.Impl.SessionImpl.GetEntityPersister(Object obj)\r\n at NHibernate.Impl.SessionImpl.IsUnsaved(Object obj, Boolean earlyInsert, Object self)\r\n at NHibernate.Impl.SessionImpl.NullifyTransientReferences(Object value, IType type, Boolean earlyInsert, Object self)\r\n at NHibernate.Impl.SessionImpl.NullifyTransientReferences(Object[] values, IType[] types, Boolean earlyInsert, Object self)\r\n at NHibernate.Impl.SessionImpl.DoSave(Object theObj, EntityKey key, IEntityPersister persister, Boolean replicate, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)\r\n at NHibernate.Impl.SessionImpl.DoSave(Object obj, Object id, IEntityPersister persister, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)\r\n at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)\r\n at NHibernate.Impl.SessionImpl.Save(Object obj)\r\n at NHibernate.Impl.SessionImpl.SaveOrUpdate(Object obj)\r\n

Any help?

_________________
Fred Stann
Solutions Architect


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 14, 2007 1:36 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Looks like a typo in your mapping:
Code:
<many-to-one name="Pet_claimant_id" class="RentAssist.Claimant" column="claimant_id"/>

should be
Code:
<many-to-one name="Pet_claimant" class="RentAssist.Claimant" column="claimant_id"/>

?

_________________
Karl Chu


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.