我的申请中有以下实体:
Member
FamilyAdvertisment
Address
在Member
实体中:
@OneToOne(cascade=CascadeType.ALL)
private Address address;
...
@OneToMany(fetch = FetchType.LAZY, mappedBy = "member")
private List<Advertisement> advertisements;
在Advertisement
实体中:
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
private Member member;
完整Address
实体:
@Entity
public class Address {
private String formattedAddress;
private double latitude;
private double longitude;
}
我正在尝试查找其成员在所需地址的20KM范围内具有地址的所有FamilyAdvertisement实例。
以下是我的想法:
QFamilyAdvertisement qFamilyAdvertisement = QFamilyAdvertisement.familyAdvertisement;
NumberPath<Double> lat = qFamilyAdvertisement.member.address.latitude;//NPE
NumberPath<Double> lng = qFamilyAdvertisement.member.address.longitude;
NumberPath<Double> distance = null;
NumberExpression<Double> formula =
(acos(cos(radians(Expressions.constant(requiredAddress.getLatitude())))
.multiply(cos(radians(lat))
.multiply(cos(radians(lng).subtract(radians(Expressions.constant(requiredAddress.getLongitude())))
.add(sin(radians(Expressions.constant(requiredAddress.getLatitude())))
.multiply(sin(radians(lat))))))))
.multiply(Expressions.constant(6371)));
List<FamilyAdvertisement> foundFamilyAdvertisements = from(qFamilyAdvertisement.member.address).where(formula.as(distance).lt(20)).list(qFamilyAdvertisement);
但是,我似乎错误地使用了NumberPath类,因为我一直在使用NPE。任何人都可以帮我解决问题吗?
编辑:我已更改了FamilyAdvertisement实体,如下所示:
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@QueryInit("address")
private Member member;
我现在得到以下例外:
java.lang.IllegalArgumentException: Only root paths are allowed for joins : familyAdvertisement.member.address
com.mysema.query.DefaultQueryMetadata.ensureRoot(DefaultQueryMetadata.java:208)
com.mysema.query.DefaultQueryMetadata.validateJoin(DefaultQueryMetadata.java:132)
com.mysema.query.DefaultQueryMetadata.addJoin(DefaultQueryMetadata.java:118)
com.mysema.query.DefaultQueryMetadata.addJoin(DefaultQueryMetadata.java:110)
com.mysema.query.support.QueryMixin.from(QueryMixin.java:161)
com.mysema.query.jpa.JPQLQueryBase.from(JPQLQueryBase.java:96)
com.mysema.query.jpa.impl.JPAQuery.from(JPAQuery.java:30)
org.springframework.data.jpa.repository.support.Querydsl.createQuery(Querydsl.java:88)
org.springframework.data.jpa.repository.support.QueryDslRepositorySupport.from(QueryDslRepositorySupport.java:94)
com.bignibou.repository.FamilyAdvertisementRepositoryImpl.performFamilyAdvertisementSearch(FamilyAdvertisementRepositoryImpl.java:64)
第64行就是这个:
List<FamilyAdvertisement> foundFamilyAdvertisements = from(qFamilyAdvertisement.member.address).where(formula.as(distance).lt(20)).list(qFamilyAdvertisement);
现在有什么问题吗?
edit2 :我忘了提及FamilyAdvertisement
延伸Advertisement
并且member
变量位于Advertisement
。
edit3 :这是我尝试使用QueryDSL重现的SQL:
select * from family_advertisement a inner join member m
on a.member = m.id
where m.address
in (
SELECT id
FROM address where
6371 *
acos( cos( radians(48.8558966) )
* cos( radians( latitude ) )
* cos( radians( longitude ) - radians(2.3622728) )
+ sin( radians(48.8558966) )
* sin( radians( latitude ) )
) < 20);
我尝试过这样的事情:
List<FamilyAdvertisement> foundFamilyAdvertisements = from(qFamilyAdvertisement).where(qFamilyAdvertisement.member.address.in(
new JPASubQuery().from(QAddress.address).where(formula.lt(20)))
).list(qFamilyAdvertisement);
上面给出了公式,但我不确定如何在QueryDSL中表达一个不相关的子查询,特别是上面的in运算符似乎有问题......
edit4 :
现在可以使用以下子查询:
List<FamilyAdvertisement> foundFamilyAdvertisements =
from(qFamilyAdvertisement).where(qFamilyAdvertisement.member.address.in(new JPASubQuery().from(QAddress.address).where(formula.lt(20)).list(QAddress.address))).list(qFamilyAdvertisement);
答案 0 :(得分:2)
此路径太长,无法进行急切初始化
qFamilyAdvertisement.member.address.latitude;
请在此处阅读有关Querydsl http://www.querydsl.com/static/querydsl/3.1.0/reference/html/ch03s04.html#d0e1699
中路径初始化的更多信息