Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Unfortunatly it does not work.
I'm just wondering if it's a bug or my mistake.
 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic