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.