Amazon

Sunday, June 17, 2012

More than one entity in Hibernate fetch

Can we do this kind of query in hibernate:select * from ticket, ticket_audit where ticket.ticket_id = ticket_audit.ticket_id;


Ticket and TicketAudit are related with OneToMany releationship, i.e. there can be many records in TicketAudit with respect to one Ticket. Below is the class definition


@Entity
@Table(name="hd_ticket")
public class Ticket implements Serializable {

private String  ticketID;
private Set auditTrails;

        @OneToMany(targetEntity=AuditTrail.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name = "TICKET_ID")
public Set getAuditTrails() {
return auditTrails;
}
public void setAuditTrails(Set auditTrails) {
this.auditTrails = auditTrails;
}
        
       @Id 
@Column(name="Ticket_Id")
public String getTicketID() {
return ticketID;
}
public void setTicketID(String ticketID) {
this.ticketID = ticketID;
}
@Column(name="Date_Created")
public String getDateCreated() {
return dateCreated;
}
public void setDateCreated(String dateCreated) {
this.dateCreated = dateCreated;
}
}


@Entity
@Table(name="hd_audit_trail")
public class AuditTrail implements Serializable {
private String ticketID;
       
       @Id
@Column(name="Ticket_Id")
public String getTicketID() {
return ticketID;
}
public void setTicketID(String ticketID) {
this.ticketID = ticketID;
}
@Column(name="Revision_Date_time")
public String getRevisionDatetime() {
return revisionDatetime;
}
public void setRevisionDatetime(String revisionDateTime) {
this.revisionDatetime = revisionDateTime;
}
}


The method definition of DAO is as below 
@SuppressWarnings("unchecked")
public List getTickets(final String userID, final String statusID) throws AppException{
if(logger.isDebugEnabled()){
logger.debug("AudtiTrial userID:"+userID);
logger.debug("getHibernateTemplate():"+getHibernateTemplate());
}
List results=null;
try {
results = (List)getHibernateTemplate().find(" from com.helpdesk.domain.Ticket ticket where creator='"+userID+"' and statusID LIKE '"+statusID+"'");
} catch (RuntimeException e) {
logger.fatal("RuntimeException", e);
throw new AppException(e);
}
if(logger.isDebugEnabled()){
logger.debug("TicketAudti details is :"+results);
}
if(results==null){
results = new ArrayList();
}
return results;
}

Although I did all the setups but I was getting below exception 

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.helpdesk.domain.Ticket.auditTrails, no session or session was closed

        @OneToMany(targetEntity=AuditTrail.class, cascade=CascadeType.ALL)

The above annotation was having one flag missing and that is, fetch=FetchType.EAGER. Add this property in your annotation and enjoy cascade data fetch. 
        @OneToMany(targetEntity=AuditTrail.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER)

Chillex.


No comments:

Post a Comment

Amazon Best Sellors

TOGAF 9.2 - STUDY [ The Open Group Architecture Framework ] - Chap 01 - Introduction

100 Feet View of TOGAF  What is Enterprise? Collection of Organization that has common set of Goals. Enterprise has People - organized by co...