hibernate以关联多对一字段做查询

public class RestaurantDto {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private long userId;
    private String name;
    private String tags;
    private String principal;
    @OneToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "location_id")
    private LocationDto location;
    @Column(name = "warehouse_id")
    private Long convergentPointId;
    @Column
    private Long rangeId;
}

如果对location_id做条件查询,能 想到的方法就是使用nativeQuery, 不知道有没有更优雅的方式

 

@Query(value = "select range_id from restaurant where location_id = ?1", nativeQuery = true)
long getRangeIdByLocationId(long loctionId);

 

 

方法二: 使用hql查询时,直接使用JoinColumn中的name字段

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "item_group_price_id")
@JsonUnwrapped
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
private ItemPriceGroupDto itemPriceGroup;

 

public List<ShopUserDto> getByGroupIds(Integer groupId){
    String hql = "select s from ShopUserDto s where item_group_price_id = :groupId";
    return entityManager.createQuery(hql, ShopUserDto.class).setParameter("groupId",groupId).getResultList();
}
posted @ 2018-06-19 19:11  車輪の唄  阅读(9)  评论(0编辑  收藏  举报  来源