Hi. I have an entity with a lazy @Basic field and it works. Now I want to find an entity in DB by id and force eager load of all basic fields (but not associations) with just one SQL statement. I thought that a combination of EntityManager.getReference() + Hibernate.initialize()
would do that, but in fact, the second method only retrieves the id, but not the other columns. (The first method works as expected and does not access the DB).
Language lang;
// nothing
lang = em.getReference(Language.class, 1L);
// select l1_0."id" from "TestCache2$Language" l1_0 where l1_0."id"=?
Hibernate.initialize(lang);
// select l1_0."code",l1_0."name" from "TestCache2$Language" l1_0 where l1_0."id"=?
lang.getName();
// nothing
lang.getCode();
@Entity
@Immutable
public static class Language {
public Language() {}
@Id
private long id;
@Basic(fetch = FetchType.LAZY)
private String code;
@Basic(fetch = FetchType.LAZY)
private String name;
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(final String code) {
this.code = code;
}
}