【问题标题】:Spring data JPA Specifications - Find all data using a key to join 3 tablesSpring data JPA Specifications - 使用键查找所有数据以连接 3 个表
【发布时间】: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


【解决方案1】:

你可以用这样的东西

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

【讨论】:

  • 我正在使用 spring data jpa 规范,我想这样做......如果可能的话,请分享一些建议......我不想写原生 sql
  • Ram Spring 数据提供@Query 注释,我更新了评论,第一个不是原生查询它是 hql。
猜你喜欢
  • 1970-01-01
  • 2019-11-28
  • 2019-02-09
  • 2018-11-30
  • 2017-11-08
  • 2020-11-23
  • 2017-11-28
  • 2018-10-01
  • 2021-12-15
相关资源
最近更新 更多