Could any one points out what I did wrong? Much appreciated.
java test codes:
Parent p = new Parent(); ParentId pidObj = new ParentId(); pidObj.setPid(3); pidObj.setVal("a value"); p.setId(pidObj);
session.load(p, pidObj);
Child c = new Child(); ChildId cidObj = New ChildId(); cidObj.setCid(10); cidObj.setVal("a value"); c.setId(cidObj);
c.setParent(p); //p.getChildren().add(c); session.save(c); session.flush();
Exception thrown as following:
Hibernate: insert into child (child_id, value, name) values (?, ?, ?) [main] ERROR JDBCExceptionReporter - Field 'parent_id' doesn't have a default value
As you could see, the problem is: when insert to child table, required field parent_id was not inclued in hibernated created SQL. I am not sure why hibernate did not use the parent_id i loaded from database (in the above case, it's 3).
Does any one have any idea to fix this? All the java classes, hibernate mappings and database tables are attached below.
Public class Child implements java.io.Serializable { private ChildId id; private String name; private Paren parent;
// setter & getter methods below // }
public class ChildId implements java.io.Serializable { private Integer cid; private String val;
// getter and setter methods //
public boolean equals(Object other) { // implements equals() // }
public int hashCode() { // implements hashCode() // } }
Public class Parent implements java.io.Serializable { private ParentId id; private String name; private Set children = new HashSet(0);
// setter & getter methods below // }
public class ParentId implements java.io.Serializable { private Integer pid; private String val; // note this column similar to that in ChildId class, same value too //
// getter and setter methods //
public boolean equals(Object other) { // implements equals() // }
public int hashCode() { // implements hashCode() // } }
<hibernate-mapping> <class name="Child" table="child"> <composite-id name="id" class="ChildId"> <key-property name="cid" type="java.lang.Integer"> <column name="child_id" /> </key-property> <key-property name="val" type="java.lang.String"> <column name="value" /> </key-property> </composite-id>
<property name="name" type="java.lang.String"> <column name="child_name" not-null="true" /> </property> <many-to-one name="parent" class="Parent" fetch="select" insert="false" update="false"> <column name="parent_id" not-null="true" /> <column name="value" not-null="true" /> </many-to-one> </class> </hibernate-mapping>
<hibernate-mapping> <class name="Parent" table="parent" dynamic-update="true"> <composite-id name="id" class="ParentId"> <key-property name="pid" type="java.lang.Integer"> <column name="parent_id" /> </key-property> <key-property name="val" type="java.lang.String"> <column name="value" /> </key-property> </composite-id>
<property name="name" type="java.lang.String"> <column name="parent_name" not-null="true" /> </property>
<set name="children" inverse="true"> <key> <column name="child_id" not-null="true" /> <column name="value" not-null="true" /> </key> <one-to-many class="Child" /> </set> </class> </hibernate-mapping>
CREATE TABLE parent ( parent_id INTEGER NOT NULL, value VARCHAR(6) NOT NULL, parent_name VARCHAR(50) NOT NULL, PRIMARY KEY(parent_id, value) );
CREATE TABLE child ( child_id INTEGER NOT NULL, value VARCHAR(6) NOT NULL, child_name VARCHAR(50) NOT NULL, parent_id INTEGER NOT NULL, PRIMARY KEY(child_id, value), FOREIGN KEY (parent_id, value) REFERENCES parent (parent_id, value) );
|