【发布时间】:2018-01-23 06:21:18
【问题描述】:
我在获取分页数据时遇到问题,我有 3 个名为 Machine、Category、Role 的表。
Machine have many-to-one relationship with Category
Category have one-to-Many relationship with Roles (one category may linked with multiple roles)
角色实体
@Table(name = "role")
public class UserRole {
private Long roleId;
private String roleName;
//getter and setters
private Set<CategoryRoleMapping> categoryRoleMappings = new HashSet<CategoryRoleMapping>(0);
@OneToMany(fetch = FetchType.LAZY, mappedBy = "category")
public Set<CategoryRoleMapping> getCategoryRoleMappings() {
return categoryRoleMappings;
}
public void setCategoryRoleMappings(Set<CategoryRoleMapping> categoryRoleMappings) {
this.categoryRoleMappings = categoryRoleMappings;
}
}
角色类别映射
@Table(name = "category_role_mapping")
public class CategoryRoleMapping implements Serializable {
private static final long serialVersionUID = 1L;
private Long crMappingId;
private UserRole userRole;
private Category category;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "CR_MAPPING_ID", unique = true, nullable = false)
public Long getCrMappingId() {
return crMappingId;
}
public void setCrMappingId(Long crMappingId) {
this.crMappingId = crMappingId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ROLE_ID", nullable = false)
public UserRole getUserRole() {
return userRole;
}
public void setUserRole(UserRole userRole) {
this.userRole = userRole;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CATEGORY_ID", nullable = false)
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
类别实体
@Table(name = "category")
public class Category implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@JsonView(DataTablesOutput.View.class)
private Long categoryId;
@JsonView(DataTablesOutput.View.class)
private String categoryName;
private Set<CategoryRoleMapping> categoryRoleMappings = new HashSet<CategoryRoleMapping>(0);
//getter and setters
@OneToMany(fetch = FetchType.LAZY, mappedBy = "category")
public Set<CategoryRoleMapping> getCategoryRoleMappings() {
return categoryRoleMappings;
}
public void setCategoryRoleMappings(Set<CategoryRoleMapping> categoryRoleMappings) {
this.categoryRoleMappings = categoryRoleMappings;
}
}
通过 category_role_mapping 实体链接的角色表和类别表。
机器实体
@Table(name = "machine")
public class Machine implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Long machineId;
private String machineName;
private Category category;
//getter and setters
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CATEGORY_ID", unique = true, nullable = false)
public Category getCategory() {
return this.category;
}
public void setCategory(Category category) {
this.category = category;
}
}
我需要根据角色 ID 从机器中选择所有记录。我正在使用 spring jpa 数据规范...请提供一些可能的建议。
【问题讨论】:
-
你能分享你的实体设计吗?
-
我用实体详细信息更新了答案
标签: java mysql spring hibernate jpa