当前位置: 首页 > 面试题库 >

JPA和Hibernate初始化非惰性集合错误

潘雅珺
2023-03-14
问题内容

在我的JavaFX应用程序中,当我对EmbeddedId类使用 Non Lazy Collection
选项时,为了保持双向关系,它将产生以下异常。经过几个小时的工作后,我仍然无法解决它。实体类如下。谢谢。

EmbeddedId类

@Embeddable
public class WorkflowStepPK implements Serializable {

    @Column(name = "STEP_SEQ_NO", nullable = false)
    private Integer stepSeqNo;

    @JoinColumn(name = "WST_ID", referencedColumnName = "ID", nullable = false)
    @ManyToOne
    private WfScriptTemplate wfScriptTemplate;
}

WorkflowStep实体 (带有EmbeddedId)

@Entity
@Table(name = "workflow_step")
public class WorkflowStep implements Serializable {

    @EmbeddedId
    private WorkflowStepPK id;

    // more fields
}

WfScriptTemplate实体 (带有双向关系)

@Entity
@Table(name = "wf_script_template")
public class WfScriptTemplate implements Serializable {

    @Id
    @Basic(optional = false)
    @Column(name = "ID")
    private Integer id;

    // Refer the filed "wfScriptTemplate" inside the Composite key of WorkflowStep entity
    @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "id.wfScriptTemplate")
    @LazyCollection(LazyCollectionOption.FALSE)
    @OrderBy(value = "stepSeqNo")
    private List<WorkflowStep> workflowStepList;
}

PostgreSQL异常

  Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
        at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
        at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:89)
        at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.getResultSet(AbstractLoadPlanBasedLoader.java:449)
        at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:202)
        at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:137)
        at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
        at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitial
izer.java:100)
        at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:693)
        at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventList
ener.java:92)
        at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1893)
        at org.hibernate.collection.internal.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:668)
        at org.hibernate.engine.internal.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:885)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:359)
        at org.hibernate.loader.Loader.doList(Loader.java:2551)
        at org.hibernate.loader.Loader.doList(Loader.java:2537)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2367)
        at org.hibernate.loader.Loader.list(Loader.java:2362)
        at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
        at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:229)
        at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1260)
        at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
        at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
        at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449)
        at org.hibernate.jpa.criteria.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:67)
        at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:251)
        at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:58)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(Reposito
ryFactorySupport.java:344)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactory
Support.java:329)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java
:136)
        ... 71 more
Caused by: org.postgresql.util.PSQLException: ERROR: column workflowst0_.stepseqno does not exist
  Position: 2741
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:273)
        at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:116)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:80)
        ... 108 more

MySQL异常

Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
        at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)
        at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:89)
        at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.getResultSet(AbstractLoadPlanBasedLoader.java:449)
        at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:202)
        at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:137)
        at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
        at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitial
izer.java:100)
        at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:693)
        at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventList
ener.java:92)
        at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1893)
        at org.hibernate.collection.internal.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:668)
        at org.hibernate.engine.internal.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:885)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:359)
        at org.hibernate.loader.Loader.doList(Loader.java:2551)
        at org.hibernate.loader.Loader.doList(Loader.java:2537)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2367)
        at org.hibernate.loader.Loader.list(Loader.java:2362)
        at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
        at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:229)
        at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1260)
        at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
        at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
        at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449)
        at org.hibernate.jpa.criteria.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:67)
        at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:330)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(Reposito
ryFactorySupport.java:344)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactory
Support.java:329)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java
:136)
        ... 71 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'workflowst0_.stepSeqNo' in 'order clause'
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
        at com.mysql.jdbc.Util.getInstance(Util.java:386)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2834)
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2156)
        at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2313)
        at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:116)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:80)
        ... 107 more

问题答案:

摘录自JPA 2.0规范的第11.1.38章“按注释排序”:

点(“。”)标记用于引用嵌入属性中的属性。与点符号一起使用的每个标识符的值是相应嵌入字段或属性的名称。

因此,您应该使用以下注释workflowStepList

@OrderBy(value = "id.stepSeqNo")
private List<WorkflowStep> workflowStepList;

从而表明stepSeqNo属于WorkflowStep可嵌入的。



 类似资料:
  • 问题内容: 在我的JavaFX应用程序中,当我对EmbeddedId类使用“ 非延迟收集” 选项时,为了保持双向关系,它将产生以下异常。经过几个小时的工作后,我仍然无法解决它。实体类如下。谢谢。 EmbeddedId类 WorkflowStep实体 (带有EmbeddedId) WfScriptTemplate实体 (具有双向关系) PostgreSQL异常 MySQL异常 问题答案: 摘录自JP

  • 我正在开发一个Struts2 Spring Hibernate webapp,我需要在检索一个对象或该对象的集合后初始化一个惰性集合。 用例 我有一个团队模型,其关系被热切地加载为员工(我认为这显然是一个集合)。反过来,员工模型有一个懒惰的关系注册表,我只需要一些特定的操作,所以我根本不需要急切地加载它。 现在。我调用我的(用Spring注入到我的Struts2控制器中),以便检索一个特定的已经加

  • 问题内容: 我正在使用JPA(Hibernate作为我的JPA提供程序)。我实际上是在尝试避免hibernate细节并使用JPA规范。我有一个初始化惰性实体的函数。不幸的是,它使用了Hibernate特定的功能。这是我的功能: 是否有任何纯JPA方式初始化实体? 问题答案: 似乎没有一种初始化实体的标准方法。 有一种标准的方法可以检查是否已初始化(完全加载)实体,这是通过PersistenceUn

  • 问题内容: 您将如何在Swift中实现以下模式? 所述类被初始化,其中包含的词典JSON数组。这些字典用于初始化类。但是,当访问或属性时,对象的初始化会延迟进行。 问题答案: 看来这个问题已经得到了很大的回答,但是回过头来看原始帖子,这是(IMHO)Swift中相对简洁的翻译。关键是您可以链接惰性属性。请注意,我同时使用了类函数和闭包- 两者都很好。

  • 我们已经用Spring 4、Hibernate、repositories和JPA开发了一个网站大约一年了。在通过endpoint检索数据时,我们使用OpenEntityManagerViewFilter保持会话打开,但最近一直在处理计划的操作,现在遇到了问题。 我们在一个类中有一个带有@Scheduled注释的方法,在另一个类中有另一个带有@Transactional注释的方法。调用方法查找文件系