【发布时间】:2012-01-17 11:12:13
【问题描述】:
我想要一些关于如何最好地布局我的 JPA 实体类的建议。假设我有 2 个表我想建模为实体、用户和角色。
Create Table users(user_id primary key,
role_id integer not null )
Create table role(role_id primary key,
description text,
)
我创建了以下两个 JPA 实体:
@Entity
@Table(name="users")
@Access(AccessType.PROPERTY)
public class User implements Serializable {
private Long userId;
private Long roleId;
private Role role;
@Column(name = "user_id")
@Id
public Long getUserId() {}
@Column(name = "role_id")
public Long getRoleId() {}
@ManyToOne()
JoinColumn(name="role_id")
public Role getRole() {}
}
角色实体:
@Entity
@Table(name="Role")
@Access(AccessType.PROPERTY)
public class Role implements Serializable {
private String description;
private Long roleId;
@Column(name = "role_id")
@Id
public Long getRoleId() {}
@Column(name = "description")
public Long getDescrition(){}
@ManyToOne()
@JoinColumn(name="role_id")
public Role getRole() {}
}
建模这种关系的正确方法是如上所述,还是我会放弃私有 Long roleId;在用户?欢迎任何建议。 当我以这种方式映射时,我收到以下错误:
org.hibernate.MappingException: Repeated column in mapping for entity:
【问题讨论】:
-
你真的想限制你的用户只有一个(或没有)
Role吗? -
@Role。在这个应用程序中,是的,他们应该只有 1 个角色。
标签: jpa