覆盖@CollectionTable对主键列的引用 [英] Override @CollectionTable reference to primary-key column

查看:491
本文介绍了覆盖@CollectionTable对主键列的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JPA文档指出 CollectionTable.joinColumns 属性是收集表的外键列,它们引用实体的主表."

JPA documentation says that CollectionTable.joinColumns property is "The foreign key columns of the collection table which reference the primary table of the entity."

我可以使其不引用实体表的主键列,而是引用具有唯一值的另一列吗?

Can I make it reference not the primary key column of the entity table, but another column which has unique values?

有一个具有以下表的遗留数据库(简化):

There is a legacy database with following tables (simplified):

staff:
  user_id (int)
  user_name (varchar, primary key)

staff_privileges:
  id_user (int)
  privileges_id (int) 
  privileges_status (boolean)

现在,我将Hibernate引入项目,并尝试将此表映射到实体:

Now I'm introducing Hibernate into the project and trying to map this tables to the entity:

@Entity
@Table(name = "staff")
public class User {

  @Column(name = "user_id")
  private Integer userId;

  @Id
  @Column(name = "user_name")
  private String userName;    // primary key column (legacy, cannot be changed)

  @ElementCollection
  @CollectionTable(name = "staff_privileges", joinColumns = @JoinColumn(name = "id_user"))
  @MapKeyColumn(name = "privileges_id")
  @Column(name = "privileges_status")
  private Map<Integer, Boolean> privileges; // privilegeId <-> isEnabled
}

不起作用,因为@CollectionTable创建对主键staff_privileges.id_user -> staff.user_name (primary key)的引用. 它应该是:staff_privileges.id_user -> staff.id_user. 有没有办法覆盖此参考?是否有任何Hibernate或JPA注释?我想保持简单,如果可能的话,不要引入任何新的Entity或Embeddable.

Won't work because @CollectionTable creates reference to primary key: staff_privileges.id_user -> staff.user_name (primary key). It should be: staff_privileges.id_user -> staff.id_user. Is there way to override this reference? Are there any Hibernate or JPA annotations for this? I would like to keep things simple and not introduce any new Entity or Embeddable if possible.

推荐答案

假设每个用户的user_id也是唯一的,那么您只需将User上的@Id批注移动到userId:您的JPA提供者不知道数据库中实际的PK是什么,只要该值是唯一的,就不会引起任何问题.

Assuming that user_id in also unique for each User then you could simply move the @Id annotation on User to userId: your JPA provider has no knowledge of what the actual PK is in the database so as long as that value is unique it would not cause any issues.

上下文略有不同,但请参见:

Slightly different context but see:

https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#No_Primary_Key

JPA ID不一定总是与数据库表主数据库匹配 键约束,也不需要主键或唯一约束.

The JPA Id does not always have to match the database table primary key constraint, nor is a primary key or a unique constraint required.

唯一的区别是,如果您从映射中对表进行反向工程,则将使用user_id而非user_name上的PK创建人员表.

The only difference would be if you were reverse engineering your tables from your mappings then the staff table would be created with PK on user_id rather than user_name.

这篇关于覆盖@CollectionTable对主键列的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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