Hi ,
I am new to hibernate...
I tried to insert child record after parent record is inserted.
Parent Table:
CREATE TABLE users (
id int(11) NOT NULL auto_increment,
username varchar(50)
password varchar(15)
PRIMARY KEY (id))
Child Table with composite key and forien key:
CREATE TABLE phone_numbers (
user_id int(11),
number_type varchar(50),
phone_number int(15),
PRIMARY KEY (user_id,number_type),
FOREIGN KEY (user_id) REFERENCES users (id)
My parent mapping xml: DBgetset.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="hibernate.DBgetset" table="users">
<id name="Id" column="id" type="long">
<generator class="native"/>
</id>
<property name="UserName" type="java.lang.String" >
<column name="username"></column>
</property>
<property name="Password" type="java.lang.String" >
<column name="password"></column>
</property>
<set name="PhoneNumbers" table="phone_numbers" inverse="false" cascade="all">
<key column="user_id" />
<one-to-many class="hibernate.PhoneNumber" />
</set>
</class>
</hibernate-mapping>
My Child mapping xml: PhoneNumber.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="hibernate.PhoneNumber" table="PHONE_NUMBERS" >
<composite-id>
<!--<key-property column="USER_ID" name="userId" type="java.lang.Long" />-->
<key-property column="NUMBER_TYPE" name="numberType" type="java.lang.String"/>
<key-property name="userId" column="user_id" type="long"/>
</composite-id>
<property name="phone" type="java.lang.Long">
<column name="phone_number" precision="22" scale="0" />
</property>
<many-to-one name="parent" class="hibernate.DBgetset" column="id" insert="false" update="false" />
</class>
</hibernate-mapping>
When i run the client file i am getting below error,
ERROR org.hibernate.util.JDBCExceptionReporter[main] - Cannot add or update a child row: a foreign key constraint fails.
I am using jboss4.0 hibernate3,
Pl help me to solve this issue.. i tried many times by midifying the code. i didnt get inserted row.
Thanks in advance.
Radha
_________________ Think Smart .....
|