使用来自持久对象的新元素更新列表
Update list with new element from persist object
我已经使用 Hibernate 从数据库中提取了一个对象并试图在列表中添加子对象,但它显示 "parent key not found" 错误。这是我的 Hibernate 映射
<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代码
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);
这是我收到的例外情况
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
我是不是配置有误?如果有人遇到这种问题,请指导我。
试试这个代码:
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);
我已经使用 Hibernate 从数据库中提取了一个对象并试图在列表中添加子对象,但它显示 "parent key not found" 错误。这是我的 Hibernate 映射
<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代码
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);
这是我收到的例外情况
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
我是不是配置有误?如果有人遇到这种问题,请指导我。
试试这个代码:
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);