-->
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.  [ 1 post ] 
Author Message
 Post subject: @JoinColumn doesn't gewnerate foreign key constraints
PostPosted: Sat Mar 21, 2009 1:12 am 
Beginner
Beginner

Joined: Sat Feb 07, 2009 12:31 am
Posts: 21
specs: hibernate 3.3.1.ga, anno 3.4.0.ga, em 3.4.0.ga

Hi,

First of all, I have read a lot of docs (maybe I missed something :( ).

Does referencedColumnName attribute of @JoinColumn need to refer only to an ID of an entity to generate a foreign key? What if I want to refer to a unique key but is not an ID (but a business key)?

Consider this example:

Code:
@Entity
public class User
{
  @Id
  private long id;
 
  @Column(unique=true)
  private String username
 
  @ManyToMany
  @JoinTable(
     name = "user_role",     
     joinColumns={
       @joinColumn(
          name="username",
          referencedColumnName="username")
          },     
     inverseJoinColumns={
       @JoinColumn(
          name="roleId",
          referencedColumnName="id")
          },
     uniqueConstraints={
        @UniqueConstraint(
          columnNames={"username","roleId"})
        }
  )
  private List<Role> roles;
}

@Entity
public class Role
{
  @Id
  private long id;

  @Column(unique=true)
  private String roleName;

  private String roleDescription;

  @ManyToMany(mappedBy="roles")
  private Collection<User> users = new ArrayList<User>();
}


Short explanation:
user_role is the bridge table where it refers a role by role's id and refers a user by username instead of user's id

The problem:
hibernate did not generate a foreign key constraint for the username field of user_role table as it would have if i used user's id instead of user's username.

And if I use roleName for the bridge table to refer to a role instead of role's id, then the bridge table would have no foreign key at all.

Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.