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: delete query with @ManyToMany relatioship
PostPosted: Wed Dec 14, 2011 8:00 am 
Newbie

Joined: Thu Nov 18, 2010 2:12 pm
Posts: 7
Good morning all,

I have a @ManyToMany relationship and I want to delete all rows from the table. (for automated testing purposes)

The remove process works when using the following code:
Code:
Setor setor = (Setor) entityManager.getReference(Setor.class, new Integer(1883));
entityManager.remove(setor);


but not when executing a delete query like:
Code:
entityManager.createQuery("delete from Setor").executeUpdate();


it fails when deleting the entity because the jointable has a fk restriction linked to the Setor primary key.
Code:
org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "FKAF61067472AD19E2: PUBLIC.SETOR_USUARIO FOREIGN KEY(SETORES_CDSETOR) REFERENCES PUBLIC.SETOR(CDSETOR)"


It really feels cumbersome to select a entity before remove it. is that a JPA limitation? or is there something wrong with my mapping?

My mapping:
Code:
@Entity
public class Setor {

@Id
@Column(name = "cdSetor")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer codigo;

@ManyToMany
private Collection<Usuario> usuarios = new LinkedHashSet<Usuario>();


Code:
public class Usuario {

@Id
@Column(name = "cdUsuario")
private String codigo;

@ManyToMany(mappedBy = "usuarios")
private Collection<Setor> setores = new LinkedHashSet<Setor>();


Top
 Profile  
 
 Post subject: Re: delete query with @ManyToMany relatioship
PostPosted: Mon Dec 19, 2011 1:15 pm 
Newbie

Joined: Thu Nov 18, 2010 2:12 pm
Posts: 7
Well, I guess I can conclude that the only approach is the first one.

thanks guys.


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.