1

I have the entity model:

@Entity
public class UserEntity {

  @Id
  @Column(name = "id")
  private String id;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "restaurant_id", nullable = false)
  @LazyToOne(value = LazyToOneOption.NO_PROXY)
  private RestaurantEntity restaurant;

  // getters & setters

}

Hibernate produces the one sql query for the following code and it's the expected behavior:

// this code gets user entry with specified user id
// as result you can see the single sql query in hibernate logs.
entityManager.find(UserEntity.class, userId);

But in case of the entity code is instrumented (by hibernate-enhance-maven-plugin) , hibernate loads all lazy relations, so the same code produces two query: One for getting user with id, the secode for getting restaurant (despite the relation is lazy).

Do you have any ideas?

kemenov
  • 389
  • 3
  • 13
  • I tried the same as yours and it doesn't load lazy entity. Maybe you access `restaurant` in toString? – Jay Smith Apr 19 '17 at 18:44
  • @JaySmith Unfortunately, no. Are you sure that you have instrumented code? – kemenov Apr 20 '17 at 03:38
  • What instrumented means? – Jay Smith Apr 20 '17 at 17:27
  • Oh.. there are the cases when you need to have ability to create proxy for "mapped by and optional" field without additional query (http://stackoverflow.com/questions/1444227/making-a-onetoone-relation-lazy/3590989#3590989). So in thi case you have to use code instrumentation. – kemenov Apr 21 '17 at 05:37

0 Answers0