Hibernate version: 3.2.6
Entity Manager version: 3.3.2
Hello there! I'm having a problem with cascade. I'm pretty sure I've done this before and worked :(
I have a class with a oneToMany mapping, with cascadetype=all and fetchtype=eager.
Well, there's no FK constraints on the DB.
I have this piece of code:
Code:
public void register(SearchModule module){
Query query = entityManager.createQuery("from SearchModule s where s.namespace = :namespace");
query.setParameter("namespace", module.getNamespace());
SearchModule registred = null;
try{
registred = (SearchModule) query.getSingleResult();
}catch (Exception e) {
}
if(registred != null){
entityManager.merge(registred);
entityManager.remove(registred);
entityManager.flush();
}
entityManager.persist(module);
}
So all I need to do is: query for a given module, if found, remove it and insert the new one (nope, can't do update... it comes from a xml file hence I have no pk)
Well, SearchModule has a oneToMany association with SearchEntity
What happens is that during the remove, only SearchModule gets dropped, his children are not removed from the DB (shouldn't ALL take care of that on cascade?)
What's even stranger is that the error, is related to the newly inserted entity:
Code:
Caused by: org.hibernate.exception.ConstraintViolationException: could not insert: [com.search.model.SearchableEntity] ...
Caused by: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry '' for key 2
Now this is really akward, even if I did not remove the previous module, since I'm just inserting a new one, his children should be getting a new id value, 2 is already an registred value on my table (with auto-increment strategy)
Can someone give me a hand on this?
Regards
BTW, all my Persistent classes are subclasses of this one:
Code:
@MappedSuperclass
public abstract class VersionableEntity implements Serializable{
private static final long serialVersionUID = -3327162735082154357L;
@Version
@Column(name="VERSION")
private Long version;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="ID", nullable=false)
private Long id;
public Long getVersion() {
return version;
}
public Long getId() {
return id;
}
public void setVersion(Long version) {
this.version = version;
}
public void setId(Long id) {
this.id = id;
}
}