I have fetched one object from database by using Hibernate and trying to add child object in list, But it shows that "parent key not found" error. Here is my Hibernate mapping
<hibernate-mapping>
<class name="com.demo.hibernate.Employee" table="EMPLOYEE">
<id name="id" type="int" column="id">
<generator class="increment" />
</id>
<property name="name" column="name" type="string"></property>
<property name="edu" column="edu" type="string"></property>
<list name="lstAddress" cascade="all">
<key column="EMPLOYEE_ID" />
<list-index column="id" />
<one-to-many class="com.demo.hibernate.Address" />
</list>
</class>
<class name="com.demo.hibernate.Address" table="ADDRESS">
<id name="id" type="int" column="id">
<generator class="assigned" />
</id>
<property name="city" column="city" type="string"></property>
<property name="state" column="state" type="string"></property>
<many-to-one name="objEmployee" column="employee_id" class="com.demo.hibernate.Address" />
</class>
</class>
</hibernate-mapping>
Java Code
Employee objEmployee = (Employee) session.get(Employee.class, 1);
Address objAdd = new Address();
objAdd.setId(200);
objAdd.setCity("Mumbai");
objAdd.setState("Maharashtra");
objAdd.setObjEmployee(objEmployee);
objEmployee.getLstAddress().add(objAdd);
session.saveOrUpdate(objEmployee);
And here is the exception which I received
8180 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 2291, SQLState: 23000
8180 [main] ERROR org.hibernate.util.JDBCExceptionReporter - ORA-02291: integrity constraint (HIBERNATE_MAP.FKE66327D438D94246) violated - parent key not found
8180 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 2291, SQLState: 23000
8180 [main] ERROR org.hibernate.util.JDBCExceptionReporter - ORA-02291: integrity constraint (HIBERNATE_MAP.FKE66327D438D94246) violated - parent key not found
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at Main.main(Main.java:77)
Caused by: java.sql.BatchUpdateException: ORA-02291: integrity constraint (HIBERNATE_MAP.FKE66327D438D94246) violated - parent key not found
at oracle.jdbc.dbaccess.DBError.throwBatchUpdateException(DBError.java:459)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:4133)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 8 more
Is I am doing any wrong configurations? Guid me if any one faced this kind of issue.
try this code:
Employee objEmployee = (Employee) session.get(Employee.class, 1);
Address objAdd = new Address();
objAdd.setId(200);
objAdd.setCity("Mumbai");
objAdd.setState("Maharashtra");
objAdd.setObjEmployee(objEmployee);
//objEmployee.getLstAddress().add(objAdd);
// save objAdd not objEmployee
session.saveOrUpdate(objAdd);