I have 2 objects: FiasHouse and FiasAddrObj. The source code is below.
The problem is when I list FiasHouse like this:
Code:
session.createCriteria(FiasHouse.class).setMaxResults(100).list()
after first request, Hibernate makes another 100 selects to fetch AddrObj's
When I move @Id annotation on aoguid in FiasAddrObj, everything works fine. How can I make lazy loading work?
Code:
@Entity @Table(name = "FIAS_HOUSE", schema = "fias")
@Where(clause = "ESTSTATUS = '2' or ESTSTATUS = '0'")
public class FiasHouse implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String houseId;
@Column(nullable = false)
private String houseGuid;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "AOGUID", referencedColumnName = "aoguid", nullable = false, updatable = false)
private FiasAddrObj fiasAddrObj;
//other fields
//setters,getters
...
}
Code:
@Entity @Table(name = "FIAS_FIAS_ADDROBJ", schema = "fias")
@Where(clause = "LIVESTATUS=1 and ACTSTATUS=1")
public class FiasAddrObj implements Serializable {
@Id
private String aoId;
@Column(nullable = false)
private String aoGuId;
//other fields
//setters,getters
...
}
P.S. Compile-time instrumentation and adding @NaturalId to guid column doesn't help. As well as adding @LazyToOne. I'm using Hibernate 4.1.5.Final, also tried with Hibernate 4.3.5.Final.