Hi all,
I am using hibernate 3 with Mysql Innodb engine.
I have a user class that references a customer table (so basically the user table has a foreign key called customer_id from the customer table).
The problem I am facing is that when I create a customer with id=0 (a customer with id=0 does not exist in the database), I get a foreign key violation exception. I understand why I am getting this error from mysql. But the requirement is that I want to create a user who does not belong to any customer (say for example the user I am creating is an admin user).
How can I add a user in the db such that the customer is set to null? The mapping for the User object is shown below (its part of a joined-class, but I am only showing whats relevant).
My question basically is that if I have an object 'A' that has a foreign key reference to another object 'B', is there no way to add a row in the table for 'A' and leaving 'B' null?
Please help!
<joined-subclass name="User" table="users">
<key column="person_id"/>
<property name="username" column="username" type="string" not-null="true" unique="true"/>
<property name="password" column="password" type="string" not-null="true"/>
<property name="enabled" column="enabled" type="boolean" not-null="true"/>
<property name="dateCreated" column="date_created" type="timestamp" not-null="true"/>
<set name="roles" lazy="false" table="user_role" cascade="save-update">
<key column="person_id" not-null="true"/>
<many-to-many class="ca.sysconet.ecourier.model.Role" column="role_id"/>
</set>
<property name="customerId" column="customer_id" type="long"/>
<many-to-one name="customer"
class="ca.sysconet.ecourier.model.Customer"
column="customer_id"
update="false"
insert="false"
lazy="false"
not-found="ignore"
/>
|