Hibernate 查询和连接很困难
我有一个实体地段有一个城市执行或城市执行独家或 如果城市执行为空,则区域执行为空,区域执行也为空 一个地区也有许多城市 这是我的桌子:Hibernate 查询和连接很困难,hibernate,jpa,spring-data,querydsl,Hibernate,Jpa,Spring Data,Querydsl,我有一个实体地段有一个城市执行或城市执行独家或 如果城市执行为空,则区域执行为空,区域执行也为空 一个地区也有许多城市 这是我的桌子: Lots ( #id_lot , ... , *city_execution* , *region_execution* ) 城市\执行FK参考id \城市 区域\执行FK引用id \区域 现在我希望在hibernate、jpa或querydsl中使用该查询,但这并不重要: 如果用户在搜索表单中输入一个区域作为参数,我可以获得所有地块,即使是在用户指定的区域中
Lots ( #id_lot , ... , *city_execution* , *region_execution* )
城市\执行FK参考id \城市
区域\执行FK引用id \区域
现在我希望在hibernate、jpa或querydsl中使用该查询,但这并不重要:
如果用户在搜索表单中输入一个区域作为参数,我可以获得所有地块,即使是在用户指定的区域中有城市的地块
我试过了,但什么都没有:
select lot from lot
left join fetch on lot.city_execution as city
left join fetch on lot.region_execution as region
where city.id_region = ?
我使用querydsl,因此可以这样写:
JPAQuery query = new JPAQuery(entityManager).from(lot)
.leftJoin( lot.regions ).fetch()
.leftJoin( lot.cities).fetch()
.where(lot.city_execution.region.id_region.eq(region_value_enter_by_user))
.distinct();
这是一个querydsl问题,因为它为我的查询生成了一个额外的连接交叉
因此,我们通过这种改变解决了这个问题
JPAQuery query = new JPAQuery(entityManager).from(lot)
.leftJoin( lot.regions ).fetch()
.leftJoin( lot.cities).fetch()
.where(lot.city_execution.region.id_region.eq(region_value_enter_by_user))
.distinct();
到
通过添加别名city.leftJoin lot.cities,city.fetch并在where中将lot.city\u执行更改为city
JPAQuery query = new JPAQuery(entityManager).from(lot)
.leftJoin( lot.regions ).fetch()
.leftJoin( lot.cities).fetch()
.where(lot.city_execution.region.id_region.eq(region_value_enter_by_user))
.distinct();
JPAQuery query = new JPAQuery(entityManager).from(lot)
.leftJoin( lot.regions ).fetch()
.leftJoin( lot.cities , city).fetch()
.where(city.region.id_region.eq(region_value_enter_by_user))
.distinct();