- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试使用包装类作为结果类型进行查询。很抱歉发了这么长的帖子,但我想让我的问题尽可能完整。根类有 3 个我要检索的列表:
@Entity
@Table(name = "cash")
public final class Cash extends BaseSimpleModel {
@Id
@SequenceGenerator(name = "id", sequenceName = "cash_seq")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "id")
private Long cashID;
@Column(length = 50, unique = true)
private String description;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "cashID")
private List<CashAllowedValue> allowedValueList;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "cashID")
private List<CashAllowedCurrency> allowedCurrencyList;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "cashID")
private List<CashAllowedCashier> allowedCashierList;
... getters and setters
}
这是我的包装类:
public class CashQueryResult extends QueryResult {
private Long cashID;
private String description;
private List<CashAllowedValue> allowedValueList;
private List<CashAllowedCurrency> allowedCurrencyList;
private List<CashAllowedCashier> allowedCashierList;
public CashQueryResult(Long id, String description, List<CashAllowedValue> allowedValueList, List<CashAllowedCurrency> allowedCurrencyList, List<CashAllowedCashier> allowedCashierList)
{
this.cashID = id;
this.description = description;
this.allowedValueList = allowedValueList;
this.allowedCurrencyList = allowedCurrencyList;
this.allowedCashierList = allowedCashierList;
}
... getters
}
这是我的查询:
public final List<CashQueryResult> getQRList(final CashQueryParameter cashQP, final QueryHint queryHint) {
List<CashQueryResult> cashQRL = null;
try {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final PredicateBuilder pb = new PredicateBuilder(cb);
final CriteriaQuery<CashQueryResult> cq = cb.createQuery(CashQueryResult.class);
final Root<Cash> cash = cq.from(getModelClass());
// Joins.
final ListJoin<Cash, CashAllowedValue> allowedValueList = cash.join(Cash_.allowedValueList, JoinType.LEFT);
final ListJoin<Cash, CashAllowedCurrency> allowedCurrencyList = cash.join(Cash_.allowedCurrencyList, JoinType.LEFT);
final ListJoin<Cash, CashAllowedCashier> allowedCashierList = cash.join(Cash_.allowedCashierList, JoinType.LEFT);
// Paths.
final Path<ValueTypeEnum> valueType = allowedValueList.get(CashAllowedValue_.valueType);
final Path<Currency> currency = allowedCurrencyList.get(CashAllowedCurrency_.currency);
final Path<IntranetUser> intranetUser = allowedCashierList.get(CashAllowedCashier_.cashier);
// Expressions. Just for testing purposes.
final Expression<List<CashAllowedValue>> cashAllowedValueListExpression = cash.get(Cash_.allowedValueList);
final Expression<List<CashAllowedCurrency>> cashAllowedCurrencyExpression = cash.get(Cash_.allowedCurrencyList);
final Expression<List<CashAllowedCashier>> cashAllowedCashierExpression = cash.get(Cash_.allowedCashierList);
cq.distinct(true);
cq.select(cb.construct(CashQueryResult.class, cash.get(Cash_.cashID), cash.get(Cash_.description),
// cash.get(Cash_.allowedValueList), cash.get(Cash_.allowedCurrencyList), cash.get(Cash_.allowedCashierList) // does not work
// cashAllowedValueListExpression, cashAllowedCurrencyExpression, cashAllowedCashierExpression // does not work
allowedValueList, allowedCurrencyList, allowedCashierList // does not work
));
// cq.multiselect(cash.get(Cash_.cashID), cash.get(Cash_.description),
// allowedValueList, allowedCurrencyList, allowedCashierList
// ); // does not work
cq.where(cb.and(pb.like(cash.get(Cash_.description), cashQP.getDescription()), pb.equal(valueType, cashQP.getValueType()), pb.equal(currency.get(Currency_.currencyID), cashQP.getCurrencyID()), pb.equal(intranetUser.get(IntranetUser_.agentID), cashQP.getIntranetUserID())));
cq.orderBy(cb.asc(cash.get(Cash_.description)));
final TypedQuery<CashQueryResult> tq = em.createQuery(cq);
tq.setFirstResult(queryHint.getFirstResult());
tq.setMaxResults(queryHint.getMaxResults());
cashQRL = tq.getResultList();
}
catch (Throwable t) {
throw new EJBException(t.getMessage());
}
return cashQRL;
}
最后,异常(它根据我尝试的选择方法而有所不同,但它总是这样的):
2011-09-12 16:12:26,165 ERROR [org.hibernate.hql.PARSER] (http-127.0.0.1-8080-2) Unable to locate appropriate constructor on class [com.ebizlink.adonis.erp.model.support.CashQueryResult] [cause=org.hibernate.PropertyNotFoundException: no appropriate constructor in class: com.ebizlink.adonis.erp.model.support.CashQueryResult]
2011-09-12 16:12:26,169 SEVERE [javax.enterprise.resource.webcontainer.jsf.context] (http-127.0.0.1-8080-2) javax.ejb.EJBException: org.hibernate.hql.ast.QuerySyntaxException:
Unable to locate appropriate constructor on class [com.ebizlink.adonis.erp.model.support.CashQueryResult]
[select distinct new com.ebizlink.adonis.erp.model.support.CashQueryResult(generatedAlias0.cashID, generatedAlias0.description, generatedAlias1, generatedAlias2, generatedAlias3)
from com.ebizlink.adonis.erp.model.Cash as generatedAlias0
left join generatedAlias0.allowedValueList as generatedAlias1
left join generatedAlias0.allowedCurrencyList as generatedAlias2
left join generatedAlias0.allowedCashierList as generatedAlias3
where ( 1=1 ) and ( 1=1 ) and ( 1=1 ) and ( 1=1 )
order by generatedAlias0.description asc]
作为引用,这里是 PredicateBuilder万一。我在网上搜索但找不到有人试图检索许多列表。我错过了一些明显的东西吗?多次提取是不行的( hibernate 错误),而且我也无法将列表设为 Eager。
另一个相关问题是:我可以这样查询,我的包装器类的列表也是实体包装器而不是实体吗? (例如:不使用 Cashiers,只获取名字和姓氏,并使用包含两个字符串的 CashierWrapper 列表)
非常感谢你,希望你能帮助我。
最佳答案
很可能你不能这样做,规范对构造函数表达式的参数说了什么:
constructor_expression ::= NEW constructor_name ( constructor_item {,constructor_item}* ) constructor_item ::= single_valued_path_expression | scalar_expression | aggregate_expression | identification_variable
并且您尝试提供给构造函数的列表 (collection_valued_field) 不是任何 constructor_item。
关于java - 带有列表的 JPA CriteriaQuery 投影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7404840/
目前,我有一个UserDao使用 CriteriaQuery 创建查询的类,例如检索 User 的实例使用其 ID 从数据库中获取(我们将该方法称为 findById )。到目前为止,它非常简单并且运
大家好,我正在 CriteriaQuery 中尝试执行此操作,我搜索了很长时间,但找不到任何可以执行此操作的方法,有人可以帮助我吗? SELECT b.name FROM Empl a LEFT OU
我知道我们可以使用 query.setHint("org.hibernate.cacheable", true) 为查询设置 hibernate 缓存,如下例所示。但是,有谁知道有什么方法可以为 Cr
示例实体: @Entity public class Employee { @Id @Column(name="EMP_ID") private long id; ..
我有 3 个有问题的表:PrblFldr --> PrblFldrAtrbtVal --> PrblTmpltAtrbt。它们之间的关系分别是“一对多”和“多对一”。 我正在使用 CriteriaBu
我有一个非常基本的问题,但找不到正确的解决方案。我想使用仅引用 JPAEntity 类的 CriteriaQuery 读取 JPAEntity 的所有记录,它应该适用于不同的 JPAEntites。
当我尝试使用 CriteriaQuery 排序数据时,它会忽略所有空值和特殊字符,例如 '-' 。这是我的代码: if ( "radical".equals((String) params.get("
我的 CriteriaQuery 仅返回一条记录,dateFrom = '2019-12-10' 和 dateTo = '2019-12-20' 应该返回两条记录我也不知道为什么。 这是我从数据库中截
谁能给我一些关于如何将这种子查询放入 CriteriaQuery 的提示? (我正在使用 JPA 2.0 - Hibernate 4.x) SELECT a, b, c FROM tableA WHE
我有一个表,其中包含它指向的实体的条形码的字符串值。不幸的是,它不是外键,它只是一个字符串,因此不存在映射。这使得连接操作变得困难。我想知道如何将这个对象连接到另一个没有定义关系的表。例如: @Ent
我有以下 EJB 结构。不要好奇Animal和Inventory,这些类在这里只是为了简化结构演示(更新:我修改了类名来构造一个更好理解的示例。IdTag 的另一个实现可能是 BarcodeId)。请
我想从数据库中获取一列之和并按两个字段分组的数据,其中参数将是动态的 我尝试使用谓词来实现,我能够获得分组依据,但总和不起作用。 Page aggregatedPaymentDetails2 = pa
假设我在 Oracle DB 中有一个列,如下所示: 带有时区的SOMETHING_TS TIMESTAMP 我想使用 CriteriaQuery 按此列进行过滤。 我可以使用 native 查询来实
我正在尝试对 JpaRepository 返回的列表进行排序。我正在使用 Specification 类而不是 @Query 注释,并且根据 this question ,我应该使用 Criteria
我有 2 个实体:Project 和 ProjectStatus。 项目实体: @Entity public class Project { @Id @GeneratedValue(s
我有一个具有 CLOB 属性的实体: public class EntityS { ... @Lob private String description; } 为了从数据库中检索某些实体
我有一个使用 SpringBoot 1.5.9 的项目(无法更改版本)。 我有这些实体: @Entity @Table(name = "data") public class DataEntity e
我有两个实体类一: @Table(name = "package") public class Package { @Id @Column(name = "package_id", i
我正在使用 JPA 和 PostgreSQL,我想创建一个 CriteriaQuery 并创建一个查询不考虑重音的地方。 例子:如果我搜索字母“a”,数据库应该返回值“ã”、“a”、“á”等。这应该发
如何使用条件查询应用左连接,我只能在互联网上找到内部连接条件。 Criteria cr = this.sessionFactory.getCurrentSession().create
我是一名优秀的程序员,十分优秀!