It's not a secret anymore!*
The moose likes Object Relational Mapping and the fly likes [Hibernate Mysql 5 EJB3 JBoss] remove() foreign key constraint fails Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "[Hibernate Mysql 5 EJB3 JBoss] remove() foreign key constraint fails" Watch "[Hibernate Mysql 5 EJB3 JBoss] remove() foreign key constraint fails" New topic
Author

[Hibernate Mysql 5 EJB3 JBoss] remove() foreign key constraint fails

Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218
Hi there,
I'm trying to delete an object which has a OneToMany Relationship

Father


Children


EJB3


Error stack trace
16:06:40,671 ERROR [JDBCExceptionReporter] Cannot delete or update a parent row: a foreign key constraint fails (`test/USR_ADDRESS`, CONSTRAINT `FK949D64094F8AB6BC` FOREIGN KEY (`user_fk_id`) REFERENCES `USR` (`id`))
16:06:40,672 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update


Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`test/USR_ADDRESS`, CONSTRAINT `FK949D64094F8AB6BC` FOREIGN KEY (`user_fk_id`) REFERENCES `USR` (`id`))
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1213)


With EJB3, the transaction is not managed by the container?
What am I missing here?

Thanks in advance


trying to decode a woman mind....
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9947
    
161

Not sure if this will help, but the Hibernate Annotation documentation mentions:

2.2.5.3.2.1. Bidirectional

To map a bidirectional one to many, with the one-to-many side as the owning side, you have to remove the mappedBy element and set the many to one @JoinColumn as insertable and updatable to false. This solution is obviously not optimized and will produce some additional UPDATE statements.

@Entity
public class Troop {
@OneToMany
@JoinColumn(name="troop_fk") //we need to duplicate the physical information
public Set<Soldier> getSoldiers() {
...
}

@Entity
public class Soldier {
@ManyToOne
@JoinColumn(name="troop_fk", insertable=false, updatable=false)
public Troop getTroop() {
...
}


Try it out and see if it works.


[My Blog] [JavaRanch Journal]
Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218

Unfortunatly it does not work.
I'm just wondering if it's a bug or my mistake.
Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218
just a doubt, on the example above the annotations are on the setter method while on my class are on the getter. Could it be the actual problem?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: [Hibernate Mysql 5 EJB3 JBoss] remove() foreign key constraint fails
 
Similar Threads
delete child rows using hibernate. (cascade?)
[OneToMany ManyToOne] null value in JoinColumn
Simple But Crazy Cascade Problem
a different object with the same identifier value was already associated with the session: [ValueObj
Deleting data from multiple tables?