Java 重新附加实体以延迟加载集合(JPA/Hibernate)

Java 重新附加实体以延迟加载集合(JPA/Hibernate),java,hibernate,jpa,persistence,entitymanager,Java,Hibernate,Jpa,Persistence,Entitymanager,有人能帮我解决一个我认为很简单的JPA问题吗。我正试图在我的JPA持久性框架中编写一个通用的延迟加载管理器,这样应用程序堆栈的更高层就可以访问延迟加载的数据,而无需处理细节 我有一个懒惰的负载管理器: public class JpaLazyLoader extends AbstractJpaDAO<Void> implements LazyLoader { public JpaLazyLoader() { super( void.class ); }

有人能帮我解决一个我认为很简单的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 );
  }
}
这只是一种错误的方式吗?如果是这样,“正确”的方法是什么?如果没有,我做错了什么


谢谢你的帮助

好的,多亏了安德烈,我的工作已经开始了

我只是没有初始化集合对象,而只是调用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
  }
}
@Transactional(readOnly=true)
public T get(ILazyGetter p_getter)抛出异常{
//将对象重新附加到会话
E l_entity=getEntityManager().merge(p_getter.getEntity());
tl_lazyData=p_getter.get(l_实体);
//仅仅获取数据并不一定会初始化它-
//根据数据类型显式初始化它。
初始性(l_lazyData);
//返回getter数据
返回l_lazyData;
}
/**
*尝试初始化实体,使其在事务中完全延迟加载
*@param p_实体
*/
受保护的void初始属性(对象p_实体){
if(p_entity!=null&&p_entity instanceof Collection){
集合l_实体=(集合)p_实体;
l_entity.size();//强制加载集合
}
}

显然您成功地获取了集合,但它是否已初始化?似乎您返回了未打开的包,当您尝试迭代该包时,它将尝试打开它,给您一个异常。迭代集合时是否有打开的会话?如果是这样,你能试着调用Hibernate.initialize()吗?是的,这正是我出错的地方。-我不想将我的JPA绑定到Hibernate,因此我确保集合已初始化。请参阅下面的评论。很高兴您能使用它,请您将此标记为选择的答案:-)
          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;
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)
@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
  }
}