gpt4 book ai didi

Null 值的 Java Criteria 构建器

转载 作者:行者123 更新时间:2023-12-01 16:54:55 26 4
gpt4 key购买 nike

我使用了java标准生成器,这是我的代码

     RepositoryFactory.emf=Persistence.createEntityManagerFactory("onezero");
String Symbol = "EURUSD";
TradingHistory tradingHistorys = RepositoryFactory.getTradingHistoryRepo().findPriceEmpty(Symbol);
String symbols = tradingHistorys.getSymbol();
Date created = tradingHistorys.getCreated_at();

public TradingHistory findPriceEmpty( final String Symbol) throws SQLException{
WhereClause<TradingHistory> conditions = new WhereClause<TradingHistory>() {

public void defineWhere(CriteriaBuilder criteriaBuilder, CriteriaQuery<TradingHistory> criteriaQuery,
Root<TradingHistory> root) {
// TODO Auto-generated method stub

criteriaQuery.select(root).where((criteriaBuilder.and(criteriaBuilder.equal(root.get("Symbol"), Symbol)
,(criteriaBuilder.equal(root.get("Price"), null)))));
}
};
return singleResult(conditions,null,0,1,0);

}

当我使用is null()时

    public TradingHistory findPriceEmpty( final String Symbol) throws SQLException{
WhereClause<TradingHistory> conditions = new WhereClause<TradingHistory>() {

public void defineWhere(CriteriaBuilder criteriaBuilder, CriteriaQuery<TradingHistory> criteriaQuery,
Root<TradingHistory> root) {
// TODO Auto-generated method stub

criteriaQuery.select(root).where((criteriaBuilder.and(criteriaBuilder.equal(root.get("Symbol"), Symbol)
,(criteriaBuilder.equal(root.get("Price").isNull(), root)))));
}
};
return singleResult(conditions,null,0,1,0);

}

我遇到了类似的错误

    java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: is near line 1, column 157 [select generatedAlias0 from com.dxtr.hibernate.model.TradingHistory as generatedAlias0 where ( generatedAlias0.Symbol=:param0 ) and ( generatedAlias0.Price is null=generatedAlias0 )]

我的问题当price = null时如何搜索?因为当我使用该代码时,我收到错误 Java 空指针异常。所以我们有问题......

请帮我解决这个问题

最佳答案

看起来您会使用isNull()如以下示例所示:

public TradingHistory findPriceEmpty( final String Symbol) throws SQLException{
WhereClause<TradingHistory> conditions = new WhereClause<TradingHistory>() {

public void defineWhere(CriteriaBuilder criteriaBuilder, CriteriaQuery<TradingHistory> criteriaQuery,
Root<TradingHistory> root) { criteriaQuery.select(root).where((criteriaBuilder.and(criteriaBuilder.equal(root.get("Symbol"), Symbol),(criteriaBuilder.isNull(root.get("Price")))));
}
};
return singleResult(conditions,null,0,1,0);

希望有帮助。如果您遇到更多问题,请发表评论。

关于Null 值的 Java Criteria 构建器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61605358/

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