如何制作复合主键(java持久化注解) [英] how to make a composite primary key (java persistence annotation)

查看:24
本文介绍了如何制作复合主键(java持久化注解)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让表user_roles定义两列(userID、roleID)作为复合主键.应该很简单,就是不记得/找不到.

How to make it so that the table user_roles defines the two columns (userID, roleID) as a composite primary key. should be easy, just can't remember/find.

user 实体中:

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "user_roles")
public List<RoleDAO> getRoles() {
    return roles;
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getUserID() {
    return userID;
}

roles实体中:

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "user_roles")
public List<UserDAO> getUsers() {
    return users;
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getRoleID() {
    return roleID;
}

谢谢.

** 更多信息

所以有第三个表user_roles(由上面自动生成),它从user实体和roleIDuserIDcode> 来自 roles 实体.现在我需要生成的表 (user_roles) 中的这两列作为复合主键.

So there is a third table user_roles (auto generated by above) that takes userID from user entity and roleID from roles entity. Now I need those two columns in the generated table (user_roles) to be a composite primary key.

推荐答案

关于如何完全按照您的要求去做,您已经在这里得到了一些很好的答案..

You've already had a few good answers here on how to do exactly as you ask..

作为参考,让我仅提及在 Hibernate 中执行此操作的推荐方法,即使用代理键作为主键,并将业务键标记为 NaturalId:

For reference let me just mention the recommended way to do this in Hibernate instead, which is to use a surrogate key as primary key, and to mark business keys as NaturalId's:

虽然我们推荐使用代理键作为主键,你应该尝试识别自然键对于所有实体.自然键是财产或财产组合这是唯一且非空的.这是也是不可变的.映射属性里面的自然键元素.休眠将生成必要的唯一密钥和可空性约束,并且作为结果,您的映射将更多自我记录.

Although we recommend the use of surrogate keys as primary keys, you should try to identify natural keys for all entities. A natural key is a property or combination of properties that is unique and non-null. It is also immutable. Map the properties of the natural key inside the element. Hibernate will generate the necessary unique key and nullability constraints and, as a result, your mapping will be more self-documenting.

建议您实施equals() 和 hashCode() 比较实体的自然键属性.

It is recommended that you implement equals() and hashCode() to compare the natural key properties of the entity.

在代码中,使用注释,这看起来像这样:

In code, using annotations, this would look something like this:

@Entity
public class UserRole {
  @Id
  @GeneratedValue
  private long id;

  @NaturalId
  private User user;
  @NaturalId
  private Role role;
}

使用它可以为您省去很多麻烦,因为您会发现何时经常需要引用/映射组合主键.

Using this will save you a lot of headaches down the road, as you'll find out when you frequently have to reference / map the composed primary key.

我很艰难地发现了这一点,最后只是放弃了与 Hibernate 的斗争,而是决定顺其自然.我完全理解这在您的情况下可能是不可能的,因为您可能正在处理遗留软件或依赖项,但我只是想提及它以供将来参考.(如果你不能使用它,也许其他人可以!)

I found this out the hard way, and in the end just gave up fighting against Hibernate and instead decided to go with the flow. I fully understand that this might not be possible in your case, as you might be dealing with legacy software or dependencies, but I just wanted to mention it for future reference. (if you can't use it maybe someone else can!)

这篇关于如何制作复合主键(java持久化注解)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆