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: primary key in ternary association
PostPosted: Thu Jun 19, 2008 1:37 pm 
Newbie

Joined: Wed Jun 18, 2008 8:38 pm
Posts: 2
Location: Seattle, WA
Hibernate version: 3.2.2.ga

Name and version of the database you are using: MySQL 5.1

Hi All,

I am using the technique described on page 308 of "Java Persistence with Hibernate" to create a ternary association. That is, I have created an @Embeddable class with two @ManyToOne associations:
Code:
    @Embeddable
    public class UserGroupPermission {
   // Constructor, getters/setters, and hashCode/equals removed for brevity
        @Parent
        private User       user;

        @ManyToOne
        @JoinColumn(name = "groupId", nullable = false, updatable = false)
        private Group      group;

        @ManyToOne
        @JoinColumn(name = "permissionId", nullable = false, updatable = false)
        private Permission permission;
    }


In the parent class I created the collection of components:

Code:
    @CollectionOfElements
    @JoinTable(name = "UserGroupPermission", joinColumns = @JoinColumn(name = "userId"))
    private List<UserGroupPermission> userGroupPermissions = new ArrayList<UserGroupPermission>();


This all works great and is almost exactly what I want except for one thing. The problem is that I need every row to be unique, ie the primary key should be all three columns of the association table. Hibernate creates a table that allows non unique combinations:

Code:
mysql> describe usergrouppermission;
+--------------+------------+------+-----+---------+-------+
| Field        | Type       | Null | Key | Default | Extra |
+--------------+------------+------+-----+---------+-------+
| userId       | bigint(20) | NO   | MUL | NULL    |       |
| groupId      | bigint(20) | NO   | MUL | NULL    |       |
| permissionId | bigint(20) | NO   | MUL | NULL    |       |
+--------------+------------+------+-----+---------+-------+


Is there any way to force hibernate to create the table such that all of the columns are part of the primary key?

Thanks!


Top
 Profile  
 
 Post subject: Use a Set instead of a List
PostPosted: Thu Jun 19, 2008 1:44 pm 
Newbie

Joined: Wed Jun 18, 2008 8:38 pm
Posts: 2
Location: Seattle, WA
Never mind, I answered my own question. The answer is to use a java.util.Set as the collection class instead of a java.util.List.

Thanks to all who might have responded! :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 10:56 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
At the very least, thanks for the update!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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.