Hi guys,
Need some help, I've been battling this for days.
I have 2 tables: "Item", "Log" (this is a one-to-many relationship)
Now the entity beans look like this:
Code:
@Entity
@IdClass(ItemPK.class)
@Table(name = "Item")
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
@Id private Integer service_no;
@Id private String mnemonic_type;
private String header;
private String member;
private String service_type;
private String dept_name;
private String username;
private String phone_no;
private String subject;
private String description;
private String priority;
private String service_status;
private Date date_created;
private String responsible_officer;
private Date date_started;
private Date date_completed;
private Date date_file_attached;
private Date timestamp;
private String case_no;
@MapsId("Log")
@OneToMany(fetch = FetchType.EAGER, targetEntity = Log.class)
@Fetch(value = FetchMode.JOIN)
//@Fetch(value = FetchMode.SELECT)
@JoinColumns({
@JoinColumn(name = "service_no", referencedColumnName = "service_no"),
@JoinColumn(name = "mnemonic_type", referencedColumnName = "mnemonic_type")
})
@OrderBy("timestamp desc")
private List<Log> Logs;
public Item() {
}
public Integer getService_no() {
return service_no;
}
public void setService_no(Integer service_no) {
this.service_no = service_no;
}
public String getMnemonic_type() {
return mnemonic_type;
}
public void setMnemonic_type(String mnemonic_type) {
this.mnemonic_type = mnemonic_type;
}
public String getHeader() {
return header;
}
public void setHeader(String header) {
this.header = header;
}
public String getMember() {
return member;
}
public void setMember(String member) {
this.member = member;
}
public String getService_type() {
return service_type;
}
public void setService_type(String service_type) {
this.service_type = service_type;
}
public String getDept_name() {
return dept_name;
}
public void setDept_name(String dept_name) {
this.dept_name = dept_name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPhone_no() {
return phone_no;
}
public void setPhone_no(String phone_no) {
this.phone_no = phone_no;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPriority() {
return priority;
}
public void setPriority(String priority) {
this.priority = priority;
}
public String getService_status() {
return service_status;
}
public void setService_status(String service_status) {
this.service_status = service_status;
}
@JsonSerialize(using = DateSerializer.class)
public Date getDate_created() {
return date_created;
}
public void setDate_created(Date date_created) {
this.date_created = date_created;
}
public String getResponsible_officer() {
return responsible_officer;
}
public void setResponsible_officer(String responsible_officer) {
this.responsible_officer = responsible_officer;
}
@JsonSerialize(using = DateSerializer.class)
public Date getDate_started() {
return date_started;
}
public void setDate_started(Date date_started) {
this.date_started = date_started;
}
@JsonSerialize(using = DateSerializer.class)
public Date getDate_completed() {
return date_completed;
}
public void setDate_completed(Date date_completed) {
this.date_completed = date_completed;
}
@JsonSerialize(using = DateSerializer.class)
public Date getDate_file_attached() {
return date_file_attached;
}
public void setDate_file_attached(Date date_file_attached) {
this.date_file_attached = date_file_attached;
}
@JsonSerialize(using = DateSerializer.class)
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public String getCase_no() {
return case_no;
}
public void setCase_no(String case_no) {
this.case_no = case_no;
}
public List<Log> getLogs() {
return Logs;
}
public void setLogs(List<Log> Logs) {
this.Logs = Logs;
}
}
And here too...
Code:
@Entity
@IdClass(LogPK.class)
@Table(name = "Log")
public class Log implements Serializable {
private static final long serialVersionUID = 1L;
@Id private Integer service_no;
@Id private String mnemonic_type;
private String username;
private String service_status;
private String responsible_officer;
private String progress_notes;
@Id private Date timestamp;
private String notes_type;
public Log() {
}
public Integer getService_no() {
return service_no;
}
public void setService_no(Integer service_no) {
this.service_no = service_no;
}
public String getMnemonic_type() {
return mnemonic_type;
}
public void setMnemonic_type(String mnemonic_type) {
this.mnemonic_type = mnemonic_type;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getService_status() {
return service_status;
}
public void setService_status(String service_status) {
this.service_status = service_status;
}
public String getResponsible_officer() {
return responsible_officer;
}
public void setResponsible_officer(String responsible_officer) {
this.responsible_officer = responsible_officer;
}
public String getProgress_notes() {
return progress_notes;
}
public void setProgress_notes(String progress_notes) {
this.progress_notes = progress_notes;
}
@JsonSerialize(using = DateSerializer.class)
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public String getNotes_type() {
return notes_type;
}
public void setNotes_type(String notes_type) {
this.notes_type = notes_type;
}
}
Now the joins on these 2 entities are fine.
When I go to do a load on Item.class through a Criteria, here's the SQL that Hibernate spits out...
Hibernate:
select
this_.mnemonic_type as mnemonic1_29_1_,
this_.service_no as service2_29_1_,
this_.case_no as case3_29_1_,
this_.date_completed as date4_29_1_,
this_.date_created as date5_29_1_,
this_.date_file_attached as date6_29_1_,
this_.date_started as date7_29_1_,
this_.dept_name as dept8_29_1_,
this_.description as descript9_29_1_,
this_.header as header29_1_,
this_.member as member29_1_,
this_.phone_no as phone12_29_1_,
this_.priority as priority29_1_,
this_.responsible_officer as respons14_29_1_,
this_.service_status as service15_29_1_,
this_.service_type as service16_29_1_,
this_.subject as subject29_1_,
this_.timestamp as timestamp29_1_,
username as username29_1_, srrequestl2_.mnemonic_type as mnemonic1_29_3_,
srrequestl2_.service_no as service2_29_3_,
srrequestl2_.mnemonic_type as mnemonic1_3_,
srrequestl2_.service_no as service2_3_,
srrequestl2_.timestamp as timestamp3_,
srrequestl2_.mnemonic_type as mnemonic1_30_0_,
srrequestl2_.service_no as service2_30_0_,
srrequestl2_.timestamp as timestamp30_0_,
srrequestl2_.notes_type as notes4_30_0_,
srrequestl2_.progress_notes as progress5_30_0_,
srrequestl2_.responsible_officer as responsi6_30_0_,
srrequestl2_.service_status as service7_30_0_,
username as username30_0_ from
item this_
left outer join
log srrequestl2_
on this_.mnemonic_type=srrequestl2_.mnemonic_type
and this_.service_no=srrequestl2_.service_no
where
this_.mnemonic_type=?
and this_.case_no=?
order by
srrequestl2_.timestamp desc
and here's the error...
35859 [http-8080-4] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 2101, SQLState: 5000A
35859 [http-8080-4] ERROR org.hibernate.util.JDBCExceptionReporter - line 1, Column 'username' found in more than one FROM list table.
Does anyone find that SQL weird (check the
BOLD lines)
Now mind you, when I re-run this with FetchMode.SELECT - it works perfectly - but there is a serious performance hit with running it like this. I would like to run it as Fetch.JOIN.
Help, what am I doing wrong?
All help would be appreciated.