I have a mysql inner join query which joins 3 tables in the database and which results the required resultset. Now i want to write the corresponding hibernate query using session.createQuery(). I have created the model class for these 3 tables in my project. No mapping associated with the 3 tables. The mysql query is given below.
select b.ID, b.Date, d.Name, IF(c.Amount < 0, c.Amount * -1, c.Amount) Amount, IF(c.Amount < 0, "Payment", "Receipt") Type from (select TransactionID from TransactionDetail where AccountID = 56) a inner join TransactionHeader b on a.TransactionID = b.ID inner join TransactionDetail c on a.TransactionID = c.TransactionID inner join Account d on c.AccountID = d.ID where c.AccountID <> 56;
Now please tell me how to write this query using hibernate session.createQuery()? Please help me.Below is the model class for the 3 tables
Accounts.java:
@Entity
@Table(name = "Account")
public class Accounts {
@Id
@GeneratedValue
@Column(name="ID")
private Integer id;
@Column(name="Name")
private String name;
public Accounts() {
super();
// TODO Auto-generated constructor stub
}
public Accounts(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
TransactionDetails.java:
@Entity
@Table(name = "TransactionDetail")
public class TransactionDetails {
@Id
@GeneratedValue
@Column(name="ID")
private Integer id;
@Column(name="TransactionID")
private Integer transactionID;
@Column(name="AccountID")
private Integer accountID;
@Column(name="Amount")
private Float amount;
public TransactionDetails() {
super();
// TODO Auto-generated constructor stub
}
public TransactionDetails(Integer id, Integer transactionID,
Integer accountID, Float amount) {
super();
this.id = id;
this.transactionID = transactionID;
this.accountID = accountID;
this.amount = amount;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getTransactionID() {
return transactionID;
}
public void setTransactionID(Integer transactionID) {
this.transactionID = transactionID;
}
public Integer getAccountID() {
return accountID;
}
public void setAccountID(Integer accountID) {
this.accountID = accountID;
}
public Float getAmount() {
return amount;
}
public void setAmount(Float amount) {
this.amount = amount;
}
}
TransactionHeaders.java:
@Entity
@Table(name = "TransactionHeader")
public class TransactionHeaders {
@Id
@GeneratedValue
@Column(name="ID")
private Integer id;
@Column(name="Date")
private Timestamp date;
public TransactionHeaders() {
super();
// TODO Auto-generated constructor stub
}
public TransactionHeaders(Integer id,Timestamp date) {
super();
this.id = id;
this.date = date;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Timestamp getDate() {
return date;
}
public void setDate(Timestamp date) {
this.date = date;
}
}