如何将此3 JOIN查询转换为Spring Data JPA命名查询方法?

时间:2016-11-26 18:07:08

标签: java spring hibernate spring-data-jpa named-query

我不是那么进入 Spring Data JPA ,我在尝试实现命名查询时遇到以下问题(由方法名定义的查询)。

我有这3个实体类:

@Entity
@Table(name = "room_tipology")
public class RoomTipology implements Serializable{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "tipology_name")
    private String name;

    @Column(name = "tipology_description")
    private String description;

    @Column(name = "time_stamp")
    private Date timeStamp;


    @OneToMany(mappedBy = "roomTipology")
    private List<Room> rooms;

    @OneToOne(mappedBy = "roomTipology")
    private RoomRate roomRate;

    // GETTER AND SETTER METHODS
}

这代表了房间的尖端,包含了这个字段

@OneToMany(mappedBy = "roomTipology")
private List<Room> rooms;

因此,它包含与特定房间提示相关联的房间列表,因此我有房间实体类:

@Entity
@Table(name = "room")
public class Room implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @ManyToOne
    @JoinColumn(name = "id_accomodation_fk", nullable = false)
    private Accomodation accomodation;

    @ManyToOne
    @JoinColumn(name = "id_room_tipology_fk", nullable = false)
    private RoomTipology roomTipology;

    @Column(name = "room_number")
    private String number;

    @Column(name = "room_name")
    private String name;

    @Column(name = "room_description")
    @Type(type="text")
    private String description;

    @Column(name = "max_people")
    private Integer maxPeople;

    @Column(name = "is_enabled")
    private Boolean isEnabled;

    // GETTER AND SETTER METHODS
}

代表住宿的房间,它包含这个带注释的字段:

@ManyToOne
@JoinColumn(name = "id_accomodation_fk", nullable = false)
private Accomodation accomodation;

最后是住宿实体类:

@Entity
@Table(name = "accomodation")
public class Accomodation implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @OneToMany(mappedBy = "accomodation")
    private List<Room> rooms;

    @Column(name = "accomodation_name")
    private String name;

    @Column(name = "description")
    @Type(type="text")
    private String description;

    // GETTER AND SETTER METHODS
}

好的,现在我有 RoomTipology 的这个Spring Data JPA存储库类:

@Repository
@Transactional(propagation = Propagation.MANDATORY)
public interface RoomTipologyDAO extends JpaRepository<RoomTipology, Long> {


}

在这里,我想定义一个命名查询方法,它返回给我一个与特定住宿相关的所有 RoomTipology 对象的列表,我使用SQL完成它并且它工作正常:

SELECT * 
FROM room_tipology as rt
JOIN room r 
ON rt.id = r.id_room_tipology_fk
JOIN accomodation a
ON r.id_accomodation_fk = a.id
WHERE a.id = 7

但现在我想在命名查询方法中翻译它(或者至少使用 HQL

我该怎么做?

1 个答案:

答案 0 :(得分:2)

请尝试:

@Repository
@Transactional(propagation = Propagation.MANDATORY)
public interface RoomTipologyDAO extends JpaRepository<RoomTipology, Long> {

   List<RoomTipology> findByRooms_Accomodation(Accomodation accomodation);

}
  

Spring Data存储库基础结构中内置的查询构建器机制对于构建对存储库实体的约束查询非常有用。该机制剥离了前缀find ... By,read ... By,query ... By,count ... By,and get ...来自方法并开始解析其余部分

     

在创建查询时,您已确保已解析的属性是托管域类的属性。但是,您也可以通过遍历嵌套属性来定义约束。

文档:Here