【问题标题】:Reattaching an entity to lazy load a collection (JPA/Hibernate)重新附加实体以延迟加载集合(JPA/Hibernate)
【发布时间】: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


【解决方案1】:

好的,感谢安德烈,我已经成功了。

我只是没有初始化集合对象,而是调用了 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
  }
}

【讨论】:

  • 很高兴您成功了,请您将此标记为已选择的答案 :-)
猜你喜欢
  • 2016-12-21
  • 1970-01-01
  • 1970-01-01
  • 2019-04-28
  • 2020-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多