我的解决方案包含 3 个不同的项目:
EJB 项目与 Netbeans 自动生成的门面来管理实体类和 persistence.xml
Class-Library,包含所有带@Entity 注释和静态编织的数据库类以及 facade ejb 的远程接口(interface)(在 EJB 和独立客户端之间共享)
主要由 Swing GUI 类组成的独立客户端
我使用 Glassfish 3.1.2、Eclipselink 2.3 作为 JPA 提供程序、Netbeans 7.1.1 和 MySQL 数据库。我配置了一个基于 persistence.xml 静态编织我的实体类的 Ant 任务。
我在用 fetch=FetchType.LAZY 修饰的实体之间有几个@OneToOne、@ManyToOne 和@ManyToMany 注释关系。
现在我得到了以下错误:
Exception in thread "Mainframe Loader" Local Exception Stack:
Exception [EclipseLink-7242] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.ValidationException
Exception Description: An attempt was made to traverse a relationship using indirection that had a null Session. This often occurs when an entity with an uninstantiated LAZY relationship is serialized and that lazy relationship is traversed after serialization. To avoid this issue, instantiate the LAZY relationship prior to serialization.
at org.eclipse.persistence.exceptions.ValidationException.instantiatingValueholderWithNullSession(ValidationException.java:998)
at org.eclipse.persistence.internal.indirection.UnitOfWorkValueHolder.instantiate(UnitOfWorkValueHolder.java:220)
at org.eclipse.persistence.internal.indirection.DatabaseValueHolder.getValue(DatabaseValueHolder.java:88)
at org.eclipse.persistence.indirection.IndirectList.buildDelegate(IndirectList.java:244)
at org.eclipse.persistence.indirection.IndirectList.getDelegate(IndirectList.java:414)
at org.eclipse.persistence.indirection.IndirectList.size(IndirectList.java:752)
at ch.lawsuite.gui.mail.PosteingangUI.updateDokumentTable(PosteingangUI.java:47)
at ch.lawsuite.gui.mail.MailboxUI.updateDokumentTables(MailboxUI.java:81)
at ch.lawsuite.gui.mail.MailboxUI.initMailboxes(MailboxUI.java:37)
at ch.lawsuite.gui.mail.MailboxUI.<init>(MailboxUI.java:23)
at ch.lawsuite.gui.MainframeUI.initModules(MainframeUI.java:191)
at ch.lawsuite.gui.login.LoginUI$MainframeLoader.run(LoginUI.java:131)
非常感谢任何帮助。我在这个问题上完全陷入僵局一个多星期了:-(
非常感谢!
最佳答案
一旦将实体传递给远程客户端 - 您就无法加载未加载的惰性属性。您需要确保它们已加载(触摸它们或其他东西)在远程传递它们的外观中。静态编织与此无关。 (好吧,自动过程与静态/动态/无编织不同——但从概念上讲,对于我们作为开发人员来说没有区别)
关于带有静态编织的 Java-EE6 : FetchType. LAZY 抛出奇怪的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10744179/