4

I use Hibernate 5.2.5 (also use kotlin and spring 4.3.5 if that matters) and I want some of the fields of my class to be loaded lazily. But the issue is that all fields are loaded immediately, I don't have any special Hibernate settings neither use 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
}

This is how I query

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

And the output:

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=?

Please advice what's wrong with my code and how to make Hibernate load properties lazily? Thank you!

roalz
  • 2,581
  • 3
  • 24
  • 41
awfun
  • 1,948
  • 3
  • 23
  • 38
  • [This answer](http://stackoverflow.com/a/36143746/4754790) provides an overview of alternatives when it comes to lazy one-to-one associations in which foreign key is in the table mapped by the child entity. – Dragan Bozanovic Mar 06 '17 at 16:51

1 Answers1

2

Hibernate cannot proxy your own object. There are at least three well known solutions for this problem: The simplest one is to fake one-to-many relationship. This will work because lazy loading of collection is much easier then lazy loading of single nullable property but generally this solution is very inconvenient if you use complex JPQL/HQL queries. The other one is to use build time bytecode instrumentation. For more details please read Hibernate documentation: 19.1.7. Using lazy property fetching. Remember that in this case you have to add @LazyToOne(LazyToOneOption.NO_PROXY) annotation to one-to-one relationship to make it lazy. Setting fetch to LAZY is not enough. The last solution is to use runtime bytecode instrumentation but it will work only for those who use Hibernate as JPA provider in full-blown JEE environment (in such case setting "hibernate.ejb.use_class_enhancer" to true should do the trick: Entity Manager Configuration) or use Hibernate with Spring configured to do runtime weaving (this might be hard to achieve on some older application servers). In this case @LazyToOne(LazyToOneOption.NO_PROXY) annotation is also required.

 @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;
 }
}

Can you try this: http://justonjava.blogspot.in/2010/09/lazy-one-to-one-and-one-to-many.html

reference: Why Lazy loading not working in one to one association?

Community
  • 1
  • 1
Shaik Elias
  • 494
  • 8
  • 16
  • First issue was methods were not `open` and that's why Hibernate could not create a proxy. The second issue was one-to-one mapping as you've told. The good description why it is so is here: http://stackoverflow.com/questions/1444227/making-a-onetoone-relation-lazy – awfun Mar 07 '17 at 12:05
  • Please quote the important parts of the source in your answer. – Eugen Pechanec Mar 08 '17 at 23:41