I have simple one to many relation, 1 parent has multiple child.
In child table .. there's a field consist parent's key as Foreign Key.
Both parent and child was saved in table, but the parent's key cannot be saved in child table.
This is my xml mapping files of parent and child
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="ex.parent" table="parent">
<id name="id" type="long" column="id_parent" unsaved-value="null">
<generator class="native"/>
</id>
<property name="sex" column="sex" type="char"/>
<set name="cats" lazy="true" inverse="true" cascade="all">
<key column="id_parent"/>
<one-to-many class="ex.Child"/>
</set>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="ex.child" table="child">
<id name="id" type="long" column="id_child" unsaved-value="null">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<many-to-one name="parent" class="ex.Parent" column="id_parent"/>
</class>
</hibernate-mapping>
In parent table, has 2 fields, ID_PARENT and SEX
In Child table, has 3 fields, ID_CHILD and NAME and ID_PARENT
and this is the error i got:
13:48:48,224 INFO SessionFactoryImpl:269 - Query language substitutions: {}
Hibernate: insert into parent (sex) values (?)
Hibernate: SELECT LAST_INSERT_ID()
Hibernate: insert into child (name, id_parent) values (?, ?)
13:48:49,015 WARN JDBCExceptionReporter:38 - SQL Error: 1048, SQLState: S1000
13:48:49,015 ERROR JDBCExceptionReporter:46 - General error, message from serve
r: "Column 'id_parent' cannot be null"
13:48:49,045 ERROR JDBCExceptionReporter:37 - Could not insert
java.sql.SQLException: General error, message from server: "Column 'id_parent' c
annot be null"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1651)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:889)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:956)
at com.mysql.jdbc.Connection.execSQL(Connection.java:1874)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java
:1700)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java
:1569)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.jav
a:504)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.jav
a:444)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:717)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:605)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1202)
at net.sf.hibernate.engine.Cascades$3.cascade(Cascades.java:88)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:258)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:298)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:341)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:739)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:605)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1202)
at test.createCat(test.java:69)
at test.doConnect(test.java:37)
at test.main(test.java:115)
createCat()net.sf.hibernate.JDBCException: Could not insert: General error, mes
sage from server: "Column 'id_parent' cannot be null"
Press any key to continue...
Why i cannot get the ID_PARENT automaticly saved in child table as FOREIGN KEY ?
|