-->
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.  [ 4 posts ] 
Author Message
 Post subject: Annotations HOW: Composite Key == Foreign Keys
PostPosted: Wed Aug 22, 2007 2:46 am 
Regular
Regular

Joined: Wed Nov 01, 2006 2:17 pm
Posts: 78
Hi,

I am using annotations to create my DB schema but I have a problem with composite primary keys.

Hibernate generates two columns consisting of the primary key of the first part of the composite and of the second part (User and Role). So far no problem, but Hibernate generates another 2 columns that are handled as foreign keys to User and Role again:

table user_role:
roleid userid user_id role_id

What I prefer is to have only two columns user_id and role_id that on the one hand represent my primary composite key and on the other hand represent foreign keys to user and role.

This is my annotated class:

Code:
@Entity
@Table(name="USER_ROLE")
public class UserRole extends BasicEntity {

    // Fields
    // Composite Key   
   @Id
     private UserRoleId id;
   
   @ManyToOne   
     private Role role;
   
   @ManyToOne   
     private Users users;

     // Constructors

    /** default constructor */
    public UserRole() {
    }
   
    // Property accessors
    public UserRoleId getId() {
        return this.id;
    }
   
    public void setId(UserRoleId id) {
        this.id = id;
    }
    public Role getRole() {
        return this.role;
    }
   
    public void setRole(Role role) {
        this.role = role;
    }
    public Users getUsers() {
        return this.users;
    }
   
    public void setUsers(Users users) {
        this.users = users;
    }
}


This is the ant build file with the crucial part:

Code:
            <annotationconfiguration configurationfile="${src}/hibernate.cfg.xml"/>

         <hbm2ddl drop="true" create="true" outputfilename="DataModel.sql" />


How to generate only role_id and user_id once?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 22, 2007 6:48 am 
Regular
Regular

Joined: Wed Nov 01, 2006 2:17 pm
Posts: 78
No idea? Why is my composite key not at the same time the referenced foreign keys?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 23, 2007 8:22 am 
Newbie

Joined: Mon Aug 20, 2007 5:14 am
Posts: 9
hi, you can try this

Code:
@Entity
@Table(name="USER_ROLE")
@IdClass(UserRoleId.class)
public class UserRole extends BasicEntity {

   
   @Id
   @ManyToOne   
   @JoinColumn(name="role_id", nullable=false)
     private Role role;
   
   @Id
   @ManyToOne 
   @JoinColumn(name="user_id", nullable=false)
     private Users users;

//+getters and setters

@Embeddable
public class UserRoleId implements Serializable {

   @ManyToOne   
   @JoinColumn(name="role_id", nullable=false)
     private Role role;
   

   @ManyToOne 
   @JoinColumn(name="user_id", nullable=false)
     private Users users;

//+getters and setters


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 24, 2007 8:18 am 
Regular
Regular

Joined: Wed Nov 01, 2006 2:17 pm
Posts: 78
Thank you very much! It worked like a charm!


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