小编典典

Hibernate忽略“惰性”获取类型并立即加载属性

hibernate

我使用Hibernate 5.2.5(如果重要的话,也可以使用kotlin和spring
4.3.5),并且我希望延迟加载类的某些字段。但是问题是所有字段都立即加载,我没有任何特殊的Hibernate设置,也没有使用Hibernate.initialize()。

@Entity(name = "task")
@Table(name = "tasks")
@NamedQueries(
        NamedQuery(name = "task.findById", query = "SELECT t FROM task AS t WHERE t.id = :id")
)
class Task {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Int? = null

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "author_id", nullable = false)
    lateinit var author: User

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "task")
    var edit: TaskEdit? = null
}

这就是我的查询方式

TaskRepoImpl:

override fun findById(id: Int): Task? {
    val task = getCurrentSession().createNamedQuery("task.findById", Task::class.java)
            .setParameter("id", id)
            .uniqueResult()
    return task
}

TaskService:

@Transactional
fun find(id: Int): Task? {
    return taskRepo.findById(id)
}

并输出:

Hibernate: select task0_.id as id1_1_, task0_.author_id as author_i3_1_ from tasks task0_ where task0_.id=?
Hibernate: select user0_.id as id1_3_0_, user0_.enabled as enabled2_3_0_, user0_.name as name3_3_0_, user0_.password as password4_3_0_ from users user0_ where user0_.id=?
Hibernate: select taskedit0_.id as id1_0_0_, taskedit0_.task_id as task_id3_0_0_, taskedit0_.text as text2_0_0_ from task_edits taskedit0_ where taskedit0_.task_id=?

请告知我的代码出了什么问题以及如何使Hibernate加载属性变得懒惰?谢谢!


阅读 297

收藏
2020-06-20

共1个答案

小编典典

Hibernate无法代理您自己的对象。此问题至少有三种众所周知的解决方案:最简单的解决方案是伪造一对多关系。这将起作用,因为延迟加载集合比延迟加载单个可为空的属性要容易得多,但是通常,如果使用复杂的JPQL
/
HQL查询,此解决方案将非常不便。另一种是使用构建时字节码检测。有关更多详细信息,请阅读Hibernate文档:19.1.7。使用懒惰的属性获取。请记住,在这种情况下,必须将@LazyToOne(LazyToOneOption.NO_PROXY)批注添加到一对一关系以使其变得懒惰。仅将获取设置为LAZY是不够的。最后一种解决方案是使用运行时字节码检测,但是它仅对在成熟的JEE环境中将Hibernate用作JPA提供程序的用户有效(在这种情况下,将“
hibernate.ejb.use_class_enhancer”设置为true应该可以解决问题:实体管理器配置),或将Hibernate与Spring配置为进行运行时编织(在某些较旧的应用程序服务器上可能很难实现)。在这种情况下,还需要@LazyToOne(LazyToOneOption.NO_PROXY)批注。

 @Entity
public class Animal implements FieldHandled {
 private Person owner;
 private FieldHandler fieldHandler;

 @OneToOne(fetch = FetchType.LAZY, optional = true, mappedBy = "animal")
 @LazyToOne(LazyToOneOption.NO_PROXY)
 public Person getOwner() {
  if (fieldHandler != null) {
   return (Person) fieldHandler.readObject(this, "owner", owner);
  }
  return owner;
 }

 public void setOwner(Person owner) {
  if (fieldHandler != null) {
   this.owner = fieldHandler.writeObject(this, "owner", this.owner, owner);
   return;
  }
  this.owner = owner;
 }

 public FieldHandler getFieldHandler() {
  return fieldHandler;
 }

 public void setFieldHandler(FieldHandler fieldHandler) {
  this.fieldHandler = fieldHandler;
 }
}

您可以尝试一下吗:http : //justonjava.blogspot.in/2010/09/lazy-one-to-one-and-one-to-
many.html

2020-06-20