Hi,
I have a parent/child relationship where I want the children to be deleted when I delete the parent. The SQL generated seems to add the extra step of nulling the parent id column on the children before deleting the children.
We have a not null constraint on the parent id column which is why we are seeing this problem - one solution is to lose the constraint...
Hibernate version:
hibernate 3.2.5, annotations 3.3.0GA
Annotations:
On parent:
Quote:
@OneToMany(cascade = {javax.persistence.CascadeType.ALL})
@Cascade({org.hibernate.annotations.CascadeType.ALL})
@JoinColumn(name = "parent_id")
private Set<Child> children = new TreeSet<Child>();
On child:
Quote:
@ManyToOne
@JoinColumn(name = "parent_id", updatable = false, insertable = false, nullable = false)
private Parent parent;
Code between sessionFactory.openSession() and session.close():Using Spring/JPA
Quote:
Resource resource = new FileSystemResource("xxx.xml");
BeanFactory factory = new XmlBeanFactory(resource);
EntityManager sess = ((EntityManagerFactory) factory.getBean("entityManagerFactory")).createEntityManager();
EntityTransaction txn = sess.getTransaction();
txn.begin();
Parent m = (Parent) sess.createQuery("select m from Parent m where parentId=38550").getResultList().get(0);
sess.remove(m);
txn.commit();
Full stack trace of any exception that occurs:Quote:
15:01:56,709 WARN [JDBCExceptionReporter] SQL Error: 1407, SQLState: 72000
15:01:56,709 ERROR [JDBCExceptionReporter] ORA-01407: cannot update ("SCHEMA"."TABLE"."COLUMN") to NULL
15:01:56,709 WARN [JDBCExceptionReporter] SQL Error: 1407, SQLState: 72000
15:01:56,709 ERROR [JDBCExceptionReporter] ORA-01407: cannot update ("SCHEMA"."TABLE"."COLUMN") to NULL
15:01:56,709 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:433)
Name and version of the database you are using:Oracle 10
The generated SQL (show_sql=true):Quote:
Hibernate: update CHILD set parent_id=null where parent_id=?
All the code does is retrieve an object and then delete it.
[/quote]