gpt4 book ai didi

java - JPA Criteria 从一些表中选择

转载 作者:行者123 更新时间:2023-11-29 21:26:17 28 4
gpt4 key购买 nike

表的结构如下:

CREATE TABLE Train
(
idTrain INT NOT NULL AUTO_INCREMENT,
nameTrain CHAR(20) NOT NULL,
PRIMARY KEY (idTrain)
);

CREATE TABLE Station
(
idStation INT NOT NULL AUTO_INCREMENT,
nameStation CHAR(50) NOT NULL,
PRIMARY KEY (idStation)
);

CREATE TABLE Schedule
(
idStation INT NOT NULL,
idTrain INT NOT NULL,
arrivalTime TIME NOT NULL,
departureTime TIME NOT NULL,
nextStation INT NOT NULL,
kmToNextStation INT NOT NULL,
PRIMARY KEY (idStation, idTrain, nextStation),
FOREIGN KEY (idStation) REFERENCES Station(idStation),
FOREIGN KEY (idTrain) REFERENCES Train(idTrain),
FOREIGN KEY (nextStation) REFERENCES Station(idStation)
);

需要使用 JPA Criteria 实现以下 sql 查询:

SELECT Station.nameStation, Schedule.arrivalTime, Schedule.departureTime, Schedule.kmToNextStation
FROM Schedule
JOIN Station
ON Station.idStation = Schedule.idStation
JOIN Train
ON Schedule.idTrain = Train.idTrain
WHERE Train.nameTrain = "268A";

这是我的尝试:

EntityManager em = EntitySupport.getEntityManager();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<ScheduleEntity> cq = builder.createQuery(ScheduleEntity.class);
Root<ScheduleEntity> root = cq.from(ScheduleEntity.class);
Join<ScheduleEntity, TrainEntity> idTrain = root.join("idTrain");
Join<ScheduleEntity, StationEntity> idStation = root.join("idStation");
cq.multiselect(root.get("arrivalTime"),
root.get("departureTime"),
idTrain.get("nameTrain"));
Query query = em.createQuery(cq);
List res = query.getResultList();
System.out.println("Result query: " + res.toString());

显然,我做错了什么,因为我收到以下错误:

Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [srt.entity.ScheduleEntity]. Expected arguments are: java.util.Date, java.util.Date, java.lang.String [select new srt.entity.ScheduleEntity(generatedAlias0.arrivalTime, generatedAlias0.departureTime, generatedAlias1.nameTrain) from srt.entity.ScheduleEntity as generatedAlias0 inner join generatedAlias0.idTrain as generatedAlias1 inner join generatedAlias0.idStation as generatedAlias2]

帮助我使用 JPA Criteria 为上述 sql 查询编写正确的代码。

最佳答案

我对 JPA Criteria 的概念是错误的。现在我完全明白了,答案如下:

EntityManager em = EntitySupport.getEntityManager();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<ScheduleEntity> cq = builder.createQuery(ScheduleEntity.class);
Root<ScheduleEntity> from = cq.from(ScheduleEntity.class);
Join<ScheduleEntity, StationEntity> idStation = from.join("idStation");
Join<ScheduleEntity, TrainEntity> idTrain = from.join("idTrain");
Predicate where = builder.equal(idTrain.get("nameTrain"), "268A");
cq.where(where);
List<ScheduleEntity> schedule = em.createQuery(cq).getResultList();
for(ScheduleEntity s : schedule) {
System.out.println(s.toString());
}

关于java - JPA Criteria 从一些表中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35492439/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com