-->
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: Cascade problems
PostPosted: Thu Jun 19, 2008 2:07 pm 
Newbie

Joined: Thu May 18, 2006 1:46 pm
Posts: 10
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;
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 20, 2008 8:51 am 
Beginner
Beginner

Joined: Wed Dec 20, 2006 11:21 am
Posts: 20
Hi,

First I am not sure you need the

entityManager.merge(registred);

line, since you will already load this object in the session via the criteria query.

I think you are correct than cascade should delete the objects previously, did you try to log to see if the registred object is effectively found? Eg, that your delete code is actually executed.


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.