-->
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: Problem with manyToMany - relationship
PostPosted: Sun Oct 04, 2009 10:12 am 
Newbie

Joined: Wed Apr 01, 2009 5:30 am
Posts: 2
Hi,
I am using hibernate 3.2 in combination with groovy and coldfusion.
In my application I created a customer-class and a permission-class represented by hibernate-entities.
Both are referenced in a bidirectional manyToMany relationship.

The code within the entities (I use the javax-lib for annotations):

customer:
Code:
...
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
   @JoinTable(name = "rel_customer_permission",
      joinColumns = {
                      @JoinColumn( name = "fk_customerID", nullable = false )
                    },
      inverseJoinColumns = {
                      @JoinColumn( name = "fk_permissionID", nullable = false )
                     }
      )
   List<Permission> permissions = new ArrayList<Permission>();



in permission-entity:
Code:
...
ManyToMany(mappedBy = "permissions", fetch = FetchType.LAZY)
List<Customer> customers = new ArrayList<Customer>();



At the first glance, this seems to work properly, because
hibernat represents this ralation in form of a regular n:m table - relationship (see following schema):

customer -> customerPermissions -> permission
---------- ---------------------- ----------
customerID customerID | permID permID

The problem is now:
And if I want to store a bunch of permissions in a customer, it works perfectly well;
but when I try to add a single permission to a customer, it adds sometimes one permission (as it should do)
and sometimes it adds the permission-object two times. (the strange thing I can not comprehend, is why it works only ever and anon)

To make things even more obscure to me, trying to remove a single permission from the list of permissions that belong to a single customer,
causes consistently a server-time out, as if a dead-lock would block the whole server.

the code-fragments within the Data-Access-Object are:

... [for adding a permission]
Code:
customer.getPermissions().add( permission ); 
getPermissionDAO().saveOrUpdate( customer ); 


... [removing a permission-object]
Code:
customer.getPermissions().remove( permission ); 
getPermissionDAO().saveOrUpdate( customer );


I don't even know if that is a hibernate issue at all (?).
What are your suggestions about this phenomenon?

Thanx for helping in advance.


Top
 Profile  
 
 Post subject: Re: Problem with manyToMany - relationship (problem solved)
PostPosted: Tue Oct 13, 2009 10:39 am 
Newbie

Joined: Wed Apr 01, 2009 5:30 am
Posts: 2
Hi,
the error was - strangly enough - on a very common place.

1. within the customer (who contains the permission-objects) I needed to prevent
lazy initialisation.

Code:
..
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL  )
    @JoinTable(name = "rel_customer_permission",
        joinColumns = [
                        @JoinColumn( name = "fk_customerID", nullable = false )
                      ],
        inverseJoinColumns = [
                        @JoinColumn( name = "fk_permissionID", nullable = false )
                       ],
        uniqueConstraints = [
                            @UniqueConstraint( columnNames = ["fk_customerID","fk_permissionID"] )
                             ]
        )
    List<Permission> permissions = new ArrayList<Permission>();




2. and I needed to overwrite the equals-function, that hibernate could compair differnt objects (permissions)
and find them equal if only there properties are equal

I still don't know why and how coldfusion/groovy (whoever) provoked an infinite loop -
and not simple pass an hibernate-error through but
now it works (free at last :-)

Alex


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.