-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Removing an entity from a 1:1 relation in JPA
PostPosted: Mon Jun 11, 2007 10:17 am 
Newbie

Joined: Mon Jun 11, 2007 9:57 am
Posts: 3
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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 11, 2007 7:33 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you need to set all references to null b.setA and a.setB then remove the object through em.remove(), exaclty like you would do in a pure OO world (em.remove() replacing the garbage collector)

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.