【发布时间】:2014-01-21 12:18:34
【问题描述】:
谁能帮我解决一个我认为很简单的 JPA 问题。我正在尝试在我的 JPA 持久性框架中编写一个通用的延迟加载管理器,因此应用程序堆栈的更高层可以访问延迟加载的数据,而无需处理细节。
我有一个延迟加载管理器:
public class JpaLazyLoader extends AbstractJpaDAO<Void> implements LazyLoader
{
public JpaLazyLoader()
{
super( void.class );
}
@Transactional(readOnly=true)
public <T,E> T get( ILazyGetter<T,E> p_getter ) throws Exception {
// reattach the object to the session
E l_entity = getEntityManager().merge( p_getter.getEntity() );
// return the getter data
return p_getter.get( l_entity );
}
}
懒惰的getter是这样的:
public interface ILazyGetter<T,E> extends Serializable
{
public E getEntity();
public T get( E p_entity ) throws Exception;
}
这个想法是这样使用的:
return m_lazyLoader.get( new ILazyGetter<Collection<Child>, Parent>() {
private static final long serialVersionUID = 1L;
public Parent getEntity() {
return getValue(); // get the parent object from somewhere
}
public Collection<Child> get( Parent p_entity ) throws Exception {
// children are a lazy-loaded Set<Child>
return p_entity.getChildren();
}
} );
Parent中的注解是这样的:
@Entity(name="parent")
public class Parent implements Serializable {
private static final long serialVersionUID = 1L;
....
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
protected Long id;
@OneToMany
@JoinTable
(
name="parent_child_associations",
joinColumns={ @JoinColumn(name="parent_id", referencedColumnName="id") },
inverseJoinColumns={ @JoinColumn(name="child_id", referencedColumnName="id", unique=true) }
)
protected Set<Child> children;
}
父对象在不同的事务中加载,然后分离。我希望将父级重新附加到另一个会话(在@transactional 位内)是一件微不足道的事情,但我无法让它工作。我在合并之前和之后尝试过乐观/悲观/无锁定,但似乎都不起作用。
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: test.Parent.children, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:566)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:186)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:545)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:124)
at org.hibernate.collection.internal.PersistentSet.iterator(PersistentSet.java:180)
这只是错误的处理方式吗?如果是这样,“正确”的方法是什么?如果没有,我做错了什么?
感谢您的帮助
【问题讨论】:
-
显然您已成功获取该集合,但它是否已初始化?似乎您退回了未打开的袋子,当您尝试遍历该袋子时,它会尝试打开它,给您一个例外。迭代集合时是否有开放会话?如果是这样,您可以尝试调用 Hibernate.initialize() 吗?
-
是的,这正是我出错的地方。 - 我不想将我的 JPA 绑定到 Hibernate,所以我确保集合已初始化。请参阅下面的评论。
标签: java hibernate jpa persistence entitymanager