Hi,
I am trying to remove the not-owning entity of a 1:1 relation. I am using JPA resp. the HibernateAnnotations implementation of JPA.
Hibernate version:
Hibernate Core 3.2.4 SP1,
Hibernate Annotation 3.3.0 GA,
Hibernate EntityManager 3.3.1 GA
Mapping documents:
@Entity
public class A {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Basic(optional=true)
@OneToOne(cascade=CascadeType.ALL)
private B b;
}
@Entity
public class B {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Basic(optional=false)
@OneToOne(mappedBy = "b")
private A a;
}
Code between em.getTransaction().begin() and em.getTransaction().commit():
b = em.find(B.class, b.getId());
a = b.getB();
b.setA(null);
em.remove(a);
Full stack trace of any exception that occurs:
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
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:146)
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)
... 20 more
Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`database/a`, CONSTRAINT `FK` FOREIGN KEY (`b_id`) REFERENCES `b` (`id`))
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1237)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:936)
at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:294)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 28 more
Name and version of the database you are using:
MySql 5
I would have expected that EITHER remove the only reference to an object removes the entity from database, OR that removing an entity via EntityManager.remove(B) also sets A's reference to B to null. But even when I do both in a transaction it does not work.
I assume that setting a referece to
Code:
null
via
Code:
a.setB(null)
has no effect. Perhaps Hibernate is thinking that setting a reference to null is the same as "nothing to do here". If so, can I turn that behaviour off?
I'd appreciate any comments and hints. Thanks!
Best regards,
Matt