繁体   English   中英

Spring数据JPA规范-使用键查找所有数据以联接3个表

[英]Spring data JPA Specifications - Find all data using a key to join 3 tables

我在获取分页数据时遇到问题,我有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数据规范...请提供一些可能的建议。

你可以这样

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数据:在计算机存储库界面中使用此查询(也许您需要对查询变量进行一些更改)

 @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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM