gpt4 book ai didi

java - 带有数据透视表的高级 JPA

转载 作者:太空宇宙 更新时间:2023-11-04 11:15:16 24 4
gpt4 key购买 nike

我有 5 个实体,User、Roles、Permissions、UserRoles、PermisssionRoles,我将如何构造一个 JPA 类来使用数据透视表获取数据 enter image description here

公共(public)类角色实现可序列化{

//@OneToMany(mappedBy = "role_id")//私有(private)集合permissionRoleCollection;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Size(max = 100)
@Column(name = "name")
private String name;
@Size(max = 50)
@Column(name = "code")
private String code;
@Size(max = 255)
@Column(name = "brief")
private String brief;
@Size(max = 8)
@Column(name = "status")
private String status;
@Basic(optional = false)
@NotNull
@Column(name = "date_created")
@Temporal(TemporalType.TIMESTAMP)
private Date dateCreated;
@Column(name = "date_updated")
@Temporal(TemporalType.TIMESTAMP)
private Date dateUpdated;

@JoinColumn(name = "created_by", referencedColumnName = "id")
@ManyToOne
private User createdBy;
@JoinColumn(name = "updated_by", referencedColumnName = "id")
@ManyToOne
private User updatedBy;

@ManyToOne
@JoinTable(name = "permission_role",
joinColumns = @JoinColumn(name = "userid",
referencedColumnName = "userid"),
inverseJoinColumns = @JoinColumn(name = "groupid",
referencedColumnName = "groupid")
)
private Collection<PermissionRole> permissionRoles;

public Roles() {
}

public Roles(Integer id) {
this.id = id;
}

public Roles(Integer id, Date dateCreated) {
this.id = id;
this.dateCreated = dateCreated;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getBrief() {
return brief;
}

public void setBrief(String brief) {
this.brief = brief;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public Date getDateCreated() {
return dateCreated;
}

public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}

public Date getDateUpdated() {
return dateUpdated;
}

public void setDateUpdated(Date dateUpdated) {
this.dateUpdated = dateUpdated;
}


public User getCreatedBy() {
return createdBy;
}

public void setCreatedBy(User createdBy) {
this.createdBy = createdBy;
}

public User getUpdatedBy() {
return updatedBy;
}

public void setUpdatedBy(User updatedBy) {
this.updatedBy = updatedBy;
}


@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Roles)) {
return false;
}
Roles other = (Roles) object;
return !((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)));
}

@Override
public String toString() {
return "myproperty.v1.db._entities.Roles[ id=" + id + " ]";
}

}

最佳答案

我想您希望在用户和角色之间以及角色和权限之间建立多对多的关系。如果您声明一个 @ManyToMany 注释,hibernate 将知道创建一个中间表...

Check this

您还可以以编程方式创建“内部”表,并从用户到内部注释@OneToMany,从角色到内部注释@OneToMany,并且与权限相同

关于java - 带有数据透视表的高级 JPA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45525225/

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