gpt4 book ai didi

java - 使用具有多态性的条件查询进行列表连接和多重选择

转载 作者:行者123 更新时间:2023-12-01 16:38:58 25 4
gpt4 key购买 nike

我想知道是否有人知道引发异常的原因?我有以下实体。是不是因为有些返回的Employees不是DriverEmployees,所以没有路线?

@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Integer id;

private String name;
}

@Entity
public class DriverEmployee extends Employee {

@OneToMany(fetch = FetchType.LAZY)
private List<Routes> routes;
}

@Entity
public class Routes {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Integer id;

private String name;

private String description;
}

CriteriaQuery<Tuple> criteria = criteriaBuilder.createQuery(Tuple.class);
Root<Employee> employee = criteria.from(Employee.class);
Root<DriverEmployee> driverEmployee = criteriaBuilder.treat(employee, DriverEmployee.class);
ListJoin<DriverEmployee, Routes> routes = driverEmployee.joinList("routes");

// Want routes to be returned as a list in the multiselect.
criteria.multiselect(employee.get("name").alias("name"), routes.alias("routes"));

TypedQuery<Tuple> query = em.createQuery(criteria);

query.getResultList().forEach((t) -> {
process(t.get("name", String.class));
processList(t.get("routes", List.class).size());
});

我收到的错误如下。有人知道我如何才能成功运行以下命令吗?

Caused by: java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode 
+-[IDENT] IdentNode: 'routes' {originalText=routes}

最佳答案

在当前示例中,您有一个元组,其中包含一个 DriverEmployee 对象和一个 Routes 对象(我建议您将此实体重命名为 Route 并设置 @Table(name = "routes")。根据 multiselect() 的 JavaDoc :

If the type of the criteria query is CriteriaQuery (i.e., a criteria query object created by either the createTupleQuery method or by passing a Tuple class argument to the createQuery method), a Tuple object corresponding to the arguments of the multiselect method, in the specified order, will be instantiated and returned for each row that results from the query execution.

所以这意味着你不能创建像 Tuple<DriverEmployee, List<Routes>> 这样的元组如何达到当前查询的这种行为的方法是您自己完成。例如:

您的方法与标准:

CriteriaQuery<Tuple> criteria = criteriaBuilder.createQuery(Tuple.class);
Root<Employee> employee = criteria.from(Employee.class);
Root<DriverEmployee> driverEmployee = criteriaBuilder.treat(employee, DriverEmployee.class);
ListJoin<DriverEmployee, Routes> routes = driverEmployee.joinList("routes");

criteria.multiselect(employee.get("name").alias("name"), routes.alias("routes"));

TypedQuery<Tuple> query = em.createQuery(criteria);

List<Tuple> resultList = query.getResultList();
Map<String, List<Routes>> resultMap = getMapFromResultList(resultList);
resultMap.entrySet().forEach((name, routesList) -> {
process(name);
processList(routesList);
});

以及获取 map 的方法:

private Map<String, List<Routes>> getMapFromResultList(List<Tuple> tuples) {
Map<String, List<Routes>> map = new HashMap<>();
tuples.forEach(tuple -> {
String name = tuple.get("name", String.class);
Routes routes = tuple.get("routes", Routes.class);

map.computeIfPresent(name, (key, value) -> {
value.add(routes);
return value;
});

map.computeIfAbsent(name, (key) -> {
List<Routes> routesList = new ArrayList<>();
routesList.add(routes);
return routesList;
});
});
return map;
}

关于java - 使用具有多态性的条件查询进行列表连接和多重选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61903766/

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