-->
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.  [ 3 posts ] 
Author Message
 Post subject: delete-orphan with a map
PostPosted: Thu Jun 21, 2007 1:55 pm 
Beginner
Beginner

Joined: Thu Jun 21, 2007 1:47 pm
Posts: 46
I'm have a map from an entity to a set of entities. I'm currently using an implementation where I create an Entity that holds the list, and a Map in the parent object that maps from an object that that container entity. I can add and retrieve objects, but when I remove an object from the list, the container object isn't deleted from the database.

This is the annotation I am using:

@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@Cascade({org.hibernate.annotations.CascadeType.ALL,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
Map<User, RoleList> getRoles() {
return this.roles;
}

I'm hoping someone can point me to:

A) A better way to map this relation

Or

B) A way to make remove() delete the item.


Top
 Profile  
 
 Post subject: Removing
PostPosted: Thu Jun 21, 2007 2:27 pm 
Newbie

Joined: Fri Apr 07, 2006 11:29 am
Posts: 17
How are you currently "removing them?" Are you setting the map to null, or are you actually calling remove?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 3:03 pm 
Beginner
Beginner

Joined: Thu Jun 21, 2007 1:47 pm
Posts: 46
I was calling remove() on the collection. My test case showed that the collection was empty but the table wasn't.

I've tried another angle on this, by creating an object to map the many-to-many-to-many relation as it's own entity:

Code:
@Entity
@IdClass(BusinessUserRole.class)
public class BusinessUserRole implements Serializable {
   private static final long serialVersionUID = -5848868659114816920L;
   
   Business business;
   User user;
   Role role;

// ... getters, setters follow ...


And in Business:

Code:
   Set<BusinessUserRole> userRoles = new HashSet<BusinessUserRole>();
   
   @OneToMany
   public Set<BusinessUserRole> getUserRoles() {
      return userRoles;
   }

   public void setUserRoles(Set<BusinessUserRole> userRoles) {
      this.userRoles = userRoles;
   }


   public void addRoleForUser(User u, Role role) {
      BusinessUserRole bur = new BusinessUserRole(this, u, role);
      userRoles.add(bur); // Does nothing if it is already there
   }
   
   public void removeRoleForUser(User u, Role role) {
      BusinessUserRole bur = new BusinessUserRole(this, u, role);
      userRoles.remove(bur);
   }



When when I remove all roles for a user, the table is empty.

However, now my queries are somewhat inefficient:


Code:
   public Set<Role> getRolesForUser(User u) {
      Set<Role> result = new HashSet<Role>();
      for(BusinessUserRole bur: userRoles) {
         if(bur.getUser().equals(u))
            result.add(bur.getRole());
      }
      return result;
   }


It seems like I may not have the expressive power I need here - I probably need to roll this into the DAO so that I can do this as a query instead, I guess.

Is there a way to map the same table into multiple collections which can be lazily fetched?



[/code]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.