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.  [ 1 post ] 
Author Message
 Post subject: Strange behavior mapping many-to-one on inserts
PostPosted: Mon Apr 07, 2008 10:23 pm 
Newbie

Joined: Wed Dec 20, 2006 12:34 pm
Posts: 13
Hibernate version: 1.2
MySQL 5.0

I have noticed that when i have an object that i want to insert which also has a many-to-one mapping an insert sql statement is done with a null value for the many-to-one mapped class and then when flushed or committed a subsequent update sql statement is executed fill the null value with the appropriate code. Is this standard behavior? By executing this way it does not allow me to put not-null constraints on the many-to-one column in the parent table. Is there a way to force it to happen in one sql statement?

Let me explain further with a small example. It is also very important to note that my many-to-one mappings use property-ref to reference a non-primary key column - This could be the culprit.
Code:
//Account Mapping
<class name="Project.Account, Project" table="account" >
    <id name="Id" column="ACCOUNT_ID" type="int">
      <generator class="native" />
    </id>
    <property name="LastName" column="PAYOR_LAST_NAME_1" type="string" length="50"/>
    <property name="FirstName" column="PAYOR_FIRST_NAME_1" type="string" length="50"/>
    <many-to-one name="AccountType" class="Project.AccountType, Project" column="ACCOUNT_TYPE" property-ref="Code" />
</class>

//Acount Type Mapping
<class name="Project.AccountType, Project" table="account_type" lazy="false" >
    <id name="Id" column="ID" type="int" length="11" >
      <generator class="native" />
    </id>
    <property name="Code" column="CODE" type="String" length="3" />
    <property name="Name" column="NAME" type="String" length="15"/>
    <property name="Description" column="DESCRIPTION" type="String" length="50"/>
</class>

As you can see i'm not using the primary key to join to the AccountType class, rather i'm using the code field. I know this is poor design but my boss insists on using meaningful alpha codes to join to lookup tables. The usage is as follows:

Code:
session.Save(acct) 'An insert statement is executed with a null value for column ACCOUNT_TYPE
session.Flush() 'An update statement is executed with the appropriate code in the ACCOUNT_TYPE column
session.Refresh(acct)
session.Close()

Once again i'm just looking to make this a one step query. Thanks for you help ahead of time.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.