重新连接一个实体以懒惰加载收集(JPA/Hibernate)



任何人都可以帮助我解决我认为很简单的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();
            }
          } );

父母的注释是这样的:

@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)

这只是错误的方式吗?如果是这样,"正确"的方式是什么?如果没有,我在做什么错?

感谢您的任何帮助

好吧,多亏了安德烈(Andre),我才能使用。

我根本没有初始化收集对象,而是在调用parent.getChildren(),它只是返回代理对象而不是强制提取。

以下更新的GET方法适用于有类似需求的任何人:

@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() );
  T l_lazyData = p_getter.get( l_entity );
  // Just getting the data doesn't necessarily initialize it - 
  // explicitly initialize it depending on the data type.
  intializeEntity( l_lazyData );
  // return the getter data
  return l_lazyData;
}
/**
 * Attempt to initialize the entity so it is fully lazy loaded while in the transaction
 * @param p_entity
 */
protected void intializeEntity( Object p_entity ) {
  if( p_entity != null && p_entity instanceof Collection ) {
    Collection<?> l_entity = (Collection<?>)p_entity;
    l_entity.size(); // force the collection to load
  }
}

最新更新