gpt4 book ai didi

java - Hibernate 将 POJO 对象作为参数传递,并使用它的属性。是否可以?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:14:23 25 4
gpt4 key购买 nike

我试图将 POJO 作为 hibernate 参数传递,但它因异常而崩溃,我实际上不明白。我试图找出原因,但没有弄清楚任何事情。是否可以在 where 条件下使用 POJO 字段?

此处查询及其参数说明:

@Query("select jc "
+ "from JobChron jc "
+ "where "
+ "jc.state=:catalogPage.state and "
+ "jc.city=:catalogPage.city and "
+ "jc.vertical=:catalogPage.vertical "
)
LinkedList<JobChron> selectOpen(
@Param("catalogPage") CatalogPage catalogPage);

我看到的根异常是:

Caused by: java.lang.NullPointerException
at org.hibernate.hql.internal.ast.HqlSqlWalker.lookupProperty(HqlSqlWalker.java:598)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.addrExpr(HqlSqlBaseWalker.java:4980)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1263)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4686)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:4159)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2104)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2029)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2029)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:796)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:597)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:131)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:93)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1836)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344)
at com.sun.proxy.$Proxy67.createQuery(Unknown Source)
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:86)
... 118 more

最佳答案

如果您使用来自 Spring Data 的 @org.springframework.data.repository.query.Query@Param:它未实现。

您的参数名称“catalogPage”和参数值 catalogPage 最终会按原样转到 org.hibernate.jpa.internal.QueryImpl.bindValue(catalogPage) -> org.hibernate.internal.AbstractQueryImpl.setParameter("catalogPage", catalogPage) 最终在 AbstractQueryImpl.expandParameterList(...)。没有支持所需功能的参数预处理。

更新:有一种方法可以通过 Spring EL 做到这一点!参见 https://spring.io/blog/2014/07/15/spel-support-in-spring-data-jpa-query-definitions

关于java - Hibernate 将 POJO 对象作为参数传递,并使用它的属性。是否可以?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38452212/

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