HI
I have a problem with lazy loading on @ManyToOne association mapping to unique (non primary key) column.
I've been looking in reference guide and in Hibernate in action but still missing something.
I have unidirectional ManyToOne mapping :EventLog to EventLogType
Code:
@Entity
@Table(name = "event_log")
@SequenceGenerator(name = "seq_evl", sequenceName = "sq_evl_id")
public class EventLog{
@Id
@Column(name = "evl_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_evl")
private Long id;
@Column(name = "evl_executor_id", nullable = false, length = 20)
private String executorId;
@Column(name = "evl_datetime", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date dateTime;
@Column(name = "evl_request_id", nullable = true, length = 100)
private String requestId;
@ManyToOne(fetch = FetchType.LAZY,optional=false)
@JoinColumn(name = "evl_elt_code", referencedColumnName = "elt_code", nullable=false)
private EventLogType type;
}
@Entity
@Table(name = "event_log_type")
@SequenceGenerator(name = "seq_elt", sequenceName = "sq_elt_id")
public class EventLogType extends AbstractDataObjectNumberId {
@Id
@Column(name = "elt_id", nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_elt")
private Long id;
@Column(name = "elt_code", nullable = false, unique = true)
private String code;
}
I have tried different settings including making association bidirectional, trying setting nullable or unique on association columns but still lazy loading does not work for this case.
if i just execute query:
Code:
session.createQuery("from EventLog where id = ?").setLong(0, exampleEventId).uniqueResult();
or
load object
Code:
session.load(EventLog.class, exampleEventId);
I see SQL select to load EventLog and then immediate additional SQL select to load associated eventLogType.
(Of course i do not make any navigation in my code from EventLog to EventLogType that could result in additional SELECT)
ASFAIR from my previous projects i had no problem but when associated was based on FK mapped to PK of parent table.
Can anyone explain me what is going on and why it does not work as expected.