如何在JPA中编写以下关系。我有6个表:产品,功能,功能,任务,权限和用户。我需要能够创建包含功能列表的Product对象模型。列表中的每个Feature对象模型都包含一个Functions列表,每个Function都包含一个Tasks列表。我没有建立这些实体中的每一个然后将它们关联在一起的问题但是当我需要通过它与Permission表的关系来过滤每个实体时,问题就出现了。我不确定这可以做到。当我将Permissions实体映射到每个其他实体时,我似乎得到了多对多的连接,并返回了太多的值。任何见解都会很棒。
Products
prod_id
user_id
Features
prod_id
feature_id
Functions
prod_id
feature_id
function_id
Tasks
prod_id
feature_id
function_id
task_id
Permissions
prod_id
feature_id
function_id
task_id
user_id
User
user_id
这是我的Permission实体类:
import java.io.Serializable;
import javax.persistence.*;
import static javax.persistence.CascadeType.ALL;
import java.math.BigDecimal;
@IdClass(menu.entity.PermissionKey.class)
@Entity
@Table(name="PERMISSIONS")
@NamedQuery(name = "findPermissionsByUserId",query = "SELECT p FROM Permission p WHERE p.userId = :userId")
public class Permission implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name="AUTH", length=1)
private String auth;
@Column(name="PERM_DESC", length=80)
private String permDesc;
@Id
@Column(name="COMPANY",
nullable = false,
insertable = false,
updatable = false,
length=2)
private String company;
@Id
@Column(name="USER_ID",
nullable = false,
insertable = false,
updatable = false,
length=30)
private String userId;
@Id
@Column(name="PROD_ID", precision=22)
private BigDecimal prodId;
@Id
@Column(name="FEATURE", precision=22)
private BigDecimal feature;
@Id
@Column(name="FUNC", precision=22)
private BigDecimal func;
@Id
@Column(name="TASK", precision=22)
private BigDecimal task;
//uni-directional one-to-one association to Product
@OneToOne(cascade=ALL, mappedBy="permission")
private Product productObj;
@OneToOne(cascade=ALL, mappedBy="permission")
private Feature featureObj;
@OneToOne(cascade=ALL, mappedBy="permission")
private Function functionObj;
@OneToOne(cascade=ALL, mappedBy="permission")
private Task taskObj;
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name="USER_ID", referencedColumnName="USER_ID"),
@JoinColumn(name="COMPANY", referencedColumnName="COMPANY")
})
private User user;
public Permission() {
}
public String getAuth() {
return this.auth;
}
public void setAuth(String auth) {
this.auth = auth;
}
public String getCompany() {
return this.company;
}
public void setCompany(String company) {
this.company = company;
}
public String getPermDesc() {
return this.permDesc;
}
public void setPermDesc(String permDesc) {
this.permDesc = permDesc;
}
public String getUserId() {
return this.userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public BigDecimal getProdId() {
return prodId;
}
public void setProdId(BigDecimal prodId) {
this.prodId = prodId;
}
public BigDecimal getFeature() {
return this.feature;
}
public void setFeature(BigDecimal feature) {
this.feature = feature;
}
public BigDecimal getFunc() {
return func;
}
public void setFunc(BigDecimal func) {
this.func = func;
}
public BigDecimal getTask() {
return task;
}
public void setTask(BigDecimal task) {
this.task = task;
}
public Product getProductObj() {
return this.productObj;
}
public void setProductObj(Product productObj) {
this.productObj = productObj;
}
public Feature getFeatureObj() {
return this.featureObj;
}
public void setFeatureObj(Feature featureObj) {
this.featureObj = featureObj;
}
public Function getFunctionObj() {
return this.functionObj;
}
public void setFunctionObj(Function functionObj) {
this.functionObj = functionObj;
}
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
public Task getTaskObj() {
return this.taskObj;
}
public void setTaskObj(Task taskObj) {
this.taskObj = taskObj;
}
}