-->
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.  [ 5 posts ] 
Author Message
 Post subject: Subclass or Join error on Insert - key's default value
PostPosted: Fri Apr 09, 2010 9:47 am 
Newbie

Joined: Fri Apr 20, 2007 5:25 pm
Posts: 5
Location: OH
I have 3 classes Player, Manager and Person. Person is the base class, Player and Manager extend Person and add specific properties to each.
I have also 3 tables sm_player, sm_manager and sm_person:

-------------------------------
sm_player / sm_manager
-------------------------------
id int PK
personId int FK to sm_person.id
....
-------------------------------

My mapping looks like:

<class name="Player" table="sm_player" lazy="false">
<id name="Id" column="id" type="Int32">
<generator class="identity"/>
</id>
...
<join table="sm_person" optional="true">
<key column="personId"/>
<property name="FirstName" column="firstName"/>
<property name="LastName" column="lastName"/>
...
</join>
</class>

Class Manager's mapping looks almost the same as Player.
My question is, do I need to specify a <generator> for <key> ? How does NHibernate know to generate a new sm_person.id when doing:

session.Save(player);

I am getting an error of course because I have not assigned personId. Do I have to save the player as a person first? Then how do I assign the personId if there is no property defined for it. I get the exact same result if I change from using <join> to using a class mapping definition for Person and sublasses for Player and Manager.
Please help.

Thanks.

Miguel.

_________________
Miguel Curi


Top
 Profile  
 
 Post subject: Re: Subclass or Join error on Insert - key's default value
PostPosted: Fri Apr 09, 2010 9:56 am 
Newbie

Joined: Fri Apr 20, 2007 5:25 pm
Posts: 5
Location: OH
OK, maybe I missunderstood the meaning of <join> <key>. I was getting this error:

NHibernate.Exceptions.GenericADOException: could not insert: [CuriMSI.SportsManager.Framework.Model.Player][SQL: INSERT INTO sm_player (throwsBats, number, height, weight, statusId) VALUES (?, ?, ?, ?, ?)] ---> MySql.Data.MySqlClient.MySqlException: Field 'personId' doesn't have a default value.

I removed the "not null" requirement on sm_player.personId and got pass that error to this one:

NHibernate.Exceptions.GenericADOException: could not insert: [CuriMSI.SportsManager.Framework.Model.Player#1][SQL: INSERT INTO sm_person (firstName, lastName, middleName, nickName, maidenName, cityOfBirth, dateOfBirth, avatarURL, biography, memberId, personId) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)] ---> MySql.Data.MySqlClient.MySqlException: Unknown column 'personId' in 'field list'.

So instead of sm_player.personId, NHibernate thinks that I am defining sm_person.personId. How do I tell NHibernate that the join is from sm_player to sm_person and not the opposite?

_________________
Miguel Curi


Top
 Profile  
 
 Post subject: Re: Subclass or Join error on Insert - key's default value
PostPosted: Fri Apr 09, 2010 10:35 am 
Newbie

Joined: Fri Apr 20, 2007 5:25 pm
Posts: 5
Location: OH
OK, inverse="true" on the join appears to have done the trick but now I am having another problem apparently from the join itself:

System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'CuriMSI.SportsManager.Framework.Model.Player'..

The NHibernate logs don't show any errors and there is no inner exceptions to see where exactly this cast occurs. I am guessing comes from Contacts relationship to person in the following mapping:

<class name="Player" table="sm_player" lazy="false">
<id name="Id" column="id" type="Int32">
<generator class="identity"/>
</id>
...
<join table="sm_person" inverse="true">
<key column="personId"/>
...
<bag name="Contacts" cascade="all" lazy="false">
<key column="personId" />
<many-to-many class="Contact"/>
</bag>
</join>
</class>

<class name="Contact" table="sm_contact" lazy="false">
<id name="Id" column="id" type="Int32">
<generator class="identity"/>
</id>
<property name="Label" column="label"/>
<property name="Value" column="value"/>
<many-to-one name="Type" class="ContactType" column="type"/>
</class>

My Player class is:

class Player : Person
{}

class Person
{
IList<Contact> Contacts { get; set; }
}

What is wrong with my mapping?
By the way I had to remove all "not null" constraint on all foreign keys referenced in my bag and join associations or the "default value not specified" error would show up.

_________________
Miguel Curi


Top
 Profile  
 
 Post subject: Re: Subclass or Join error on Insert - key's default value
PostPosted: Mon Apr 12, 2010 1:48 pm 
Newbie

Joined: Fri Apr 20, 2007 5:25 pm
Posts: 5
Location: OH
Hello!!! .. anyone there?

_________________
Miguel Curi


Top
 Profile  
 
 Post subject: Re: Subclass or Join error on Insert - key's default value
PostPosted: Mon Apr 12, 2010 8:11 pm 
Newbie

Joined: Fri Apr 20, 2007 5:25 pm
Posts: 5
Location: OH
OK, figured it all out.. :).
I was expecting session.Save() to return the object instead of just the id.
Problem solved and everything seems to work as it should.
Thanks to myself for helping.. myself.. fix the issue..:).

_________________
Miguel Curi


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