gpt4 book ai didi

hibernate - JPA 外键列名称

转载 作者:行者123 更新时间:2023-12-03 07:01:06 25 4
gpt4 key购买 nike

@Entity
public class User {
private String mail;
private String password;

@OneToMany(mappedBy="user")
private List<UserGroup> userGroups;
}

@Embeddable
public class UserGroupPK {
private String mail;
private String role;
}

@Entity
public class UserGroup {
@EmbeddedId
private UserGroupPK id;
private String field;

@ManyToOne
@MapsId("mail")
private User user;
}

Hibernate 创建两个表:user(mail、password) 和 usergroup(user_mail、role、field)。

我需要将 user_mail 列重命名为 mail。我尝试在 UserGroupPK 中添加 @Column(name="mail") 但什么也没有。

最佳答案

我认为,以下应该有效:

@Entity
public class UserGroup {
@EmbeddedId
private UserGroupPK id;
private String field;

@ManyToOne(referencedColumnName = "mail")
@JoinColumn(name = "mail")
private User user;
}

参见相应段落5.1.7.1. Using a foreign key or an association table在 hibernate 文档中:

The @JoinColumn attribute is optional, the default value(s) is the concatenation of the name of the relationship in the owner side, _ (underscore), and the name of the primary key column in the owned side. In this example [user_mail] because the property name is [user] and the column id [(actually, the referencedColumnName)] of [User] is [mail].

(我替换了 [] 括号中的值。)

请注意,您的 User 实体没有主键,这可能不是一个好主意。您想实现什么目标?

关于hibernate - JPA 外键列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5524528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com