我正在为汽车经销商开发Web应用程序。我有一个Car类,其中包含一组安全枚举。使用JPA Criteria API查询Enum的ElementCollection
public class Car {
@Id
@GeneratedValue
private Long id;
@NotNull(message = "{year}")
@Min(value = 1950)
@Max(value = 2020)
@Column(nullable = false)
private int year;
@NotNull()
@Column(nullable = false)
private String make;
@NotNull()
@Column(nullable = false)
private String model;
@NotNull()
@Min(value = 0)
@Max(value = 1000000)
@Column(nullable = false)
private int kilometres;
@Column(nullable = false)
private int price;
@NotNull()
@Enumerated(EnumType.STRING)
private Gearbox gearbox;
@ElementCollection(fetch = FetchType.EAGER)
@Enumerated(EnumType.STRING)
@CollectionTable(name="SECURITY")
@Column(name="TYPE")
private Set<Security> securityList = new HashSet<Security>();
@NotNull()
@Column(nullable = false)
private String description;
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, orphanRemoval = true)
private List<Picture> pictureList = new ArrayList<Picture>();
// Getters and setters + help methods..
安全枚举是这样的:
public enum Security {
ABS("abs"),
AIRBAG("airbag"),
ANTISPIN("antispin"),
CENTRAL_LOCKING("centralLocking"),
REMOTE_ALARM("remoteAlarm"),
FOUR_WHEEL("fourWheel"),
PARKING_ASSISTANCE("parkingAssistance"),
SERVICE_MANUAL("serviceManual"),
STABILITY_CONTROL("stabilityControl"),
XENON_LIGHT("xenonLight");
private String label;
private Security(String label) {
}
public String getLabel() {
return label;
}
}
在Web应用程序中,我将创建一个搜索页面,用户能够定义所需的Securitiy零部件和制造商模式(让现场汽车类)。例如,用户可能会搜索具有符合“大众汽车”制造模式的汽车,而安全性至少为ABS和REMOTE_ALARM。
我的问题是,我不知道如何使用条件API创建查询。我想它应该开始像:
public List<Car> searchCars(String makePattern, Set<Security> requiredSecuirtySet) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Car> cq = cb.createQuery(Car.class);
Root<Car> _car = cq.from(Car.class);
// Give me some help here please =)
return em.createQuery(cq).getResultList();
}
你能帮我吗?我还有一个Car类的元模型。
此致敬意!
您需要使用where子句。其中car.make =:make等。您还可以在查询中传递集合,以便查询具有某些安全功能的汽车。 – siebz0r
感谢您的回答!在searchCar中,我传递了一组必需的安全枚举。所以我想这就是你的意思,问题是我不知道如何执行它=( – kungcc