我将以下JPQL查询添加到我的存储库后,我的代码无法编译,因为它似乎与d.car c子句有问题:
public interface DriverRepository extends CrudRepository<DriverDO, Long>
{
@Query("SELECT d FROM DriverDO d JOIN d.car WHERE (d.username = :username || :username is null) "
+ "and (d.onlineStatus = :onlineStatus || :onlineStatus is null) "
+ "and (d.onlineStatus = :onlineStatus || :onlineStatus is null) "
+ "and (c.licensePlate = :licensePlate || :licensePlate is null) "
+ "and (c.rating = :rating || :rating is null) "
+ "and (c.convertible = :convertible || :convertible is null) "
+ "and (c.engineType = :engineType || :engineType is null) ")
List<DriverDO> findDriversByAttributes(@Param("username") String username,
@Param("onlineStatus") OnlineStatus onlineStatus,
@Param("licensePlate") String licensePlate,
@Param("rating") Integer rating,
@Param("convertible") Boolean convertible,
@Param("engineType") EngineType engineType);
}
我已经在互联网上随处可见,似乎我起诉的语法是正确的,因为我想加入我实体中的汽车。
以下是我的实体:
@Entity
@Table(
name = "driver",
uniqueConstraints = @UniqueConstraint(name = "uc_username", columnNames = {"username"})
)
public class DriverDO
{
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
@NotNull(message = "Username can not be null!")
private String username;
@Column(nullable = false)
@NotNull(message = "Password can not be null!")
private String password;
@Enumerated(EnumType.STRING)
@Column(nullable = false)
private OnlineStatus onlineStatus;
/** One to one relationship with car */
@OneToOne(fetch = FetchType.EAGER, optional = true)
@JoinColumn(name = "car_id", nullable = true)
private CarDO car;
...
汽车实体:
@Entity
@Table(
name = "car",
uniqueConstraints = @UniqueConstraint(name = "uc_license_plate", columnNames = {"license_plate"})
)
public class CarDO {
@Id
@GeneratedValue
private Long id;
@Column(name = "license_plate", nullable = false)
@NotNull(message = "License plate can not be null!")
private String licensePlate;
@Column(nullable = false)
private Boolean convertible;
private Integer rating;
@Enumerated(EnumType.STRING)
@Column(name = "engine_type", nullable = false)
private EngineType engineType;
@OneToOne(mappedBy = "car", fetch = FetchType.EAGER)
private DriverDO driver;
@ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "manufacturer_id", nullable = false)
private ManufacturerDO manufacturer;
private CarDO() {
}
...
}
我已经尝试了很多方法来构建查询,但到目前为止似乎都没有。我不知道如何才能使加入工作。我在这里尝试完成的是我在REST GET方法中获得了一堆可选参数,并且我试图搜索通过匹配传递的参数(如果它们不为空)。我不知道我的方法是否正确,但到目前为止我甚至无法编写代码来编写解决方案。另外你可能认为我做过错误是将驾驶员实体中的汽车视为可空,但这是一个要求,因为驾驶员可以在没有汽车的情况下存在,而汽车可以没有驾驶员,但只有一辆汽车可以属于驾驶员。
这是我得到的错误:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: = near line 1, column 79 [SELECT d FROM com.mytaxi.domainobject.DriverDO d JOIN d.car WHERE (d.username = :username || :username is null) and (d.onlineStatus = :onlineStatus || :onlineStatus is null) and (d.onlineStatus = :onlineStatus || :onlineStatus is null) and (c.licensePlate = :licensePlate || :licensePlate is null) and (c.rating = :rating || :rating is null) and (c.convertible = :convertible || :convertible is null) and (c.engineType = :engineType || :engineType is null) and (c.manufacturer = :manufacturer || :manufacturer is null) ]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:91) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:272) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:189) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:141) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:77) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:553) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:662) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
... 81 common frames omitted