I have a problem to get paginated data, I have 3 tables named 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)
ROLE ENTITY
@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;
}
}
ROLE CATEGORY MAPPING
@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;
}
}
CATEGORY ENTITY
@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;
}
}
role table and category table linked via category_role_mapping entity.
MACHINE ENTITY
@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;
}
}
I need to select all records from machine based on role id. I am using spring jpa data specifications... Please Provide some possible suggestions.
You can go with something like this
List<Machine> machine = entityManager.createQuery(
"select m " +
"from Machine m " +
"inner join fetch m.category c " +
"inner join fetch c.categoryRoleMappings crm " +
"inner join fetch crm.userRole ur " +
"where ur.id = :roleId", Machine.class)
.setParameter("roleId", roleId)
Spring data: use this query in your machine repository interface (maybe you need some changes in query variable)
@Query(value = "select m from Machine m inner join fetch m.category c inner join fetch c.categoryRoleMappings crm inner join fetch crm.userRole ur where ur.id= :roleId")
List<Machine> machine = findByRoleId(@Param("roleId") long roleId));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.