私は外部キーで結合された3つのテーブルを持っています。私はすべてのテーブルに参加してデータを取得する必要があります。以下は私の3つのエンティティクラスです。springとhibernateを使用して3つのテーブルからデータをフェッチする方法は?
Stock.java
@Entity
public class Stock {
private int id;
private Date entryDate;
private int entryUserId;
private BigDecimal parValue;
private int status;
private Timestamp statusChgDate;
private int statusChgUserId;
private String stockName;
private String stockSymbol;
@Id
@Column(name = "Id", nullable = false)
public int getId() {
return id;
}
@Basic
@Column(name = "EntryDate", nullable = false)
public Date getEntryDate() {
return entryDate;
}
@Basic
@Column(name = "EntryUserID", nullable = false)
public int getEntryUserId() {
return entryUserId;
}
@Basic
@Column(name = "ParValue", nullable = false, precision = 2)
public BigDecimal getParValue() {
return parValue;
}
@Basic
@Column(name = "Status", nullable = false)
public int getStatus() {
return status;
}
@Basic
@Column(name = "StatusChgDate", nullable = false)
public Timestamp getStatusChgDate() {
return statusChgDate;
}
@Basic
@Column(name = "StatusChgUserID", nullable = false)
public int getStatusChgUserId() {
return statusChgUserId;
}
@Basic
@Column(name = "StockName", nullable = false, length = 100)
public String getStockName() {
return stockName;
}
@Basic
@Column(name = "StockSymbol", nullable = false, length = 15)
public String getStockSymbol() {
return stockSymbol;
}
... setters ...
}
StockDetl.java
@Entity
public class StockPriceDetl {
private int id;
private BigDecimal amount;
private BigDecimal closingPrice;
private BigDecimal diffAmount;
private BigDecimal maxPrice;
private BigDecimal minPrice;
private int numberOfTransaction;
private BigDecimal previousClosingPrice;
private Timestamp statusChgDate;
private int statusChgUserId;
private int tradedShares;
private Stock stockByStockId;
@Id
@Column(name = "Id", nullable = false)
public int getId() {
return id;
}
@Basic
@Column(name = "Amount", nullable = false, precision = 2)
public BigDecimal getAmount() {
return amount;
}
@Basic
@Column(name = "ClosingPrice", nullable = false, precision = 2)
public BigDecimal getClosingPrice() {
return closingPrice;
}
@Basic
@Column(name = "DiffAmount", nullable = false, precision = 2)
public BigDecimal getDiffAmount() {
return diffAmount;
}
@Basic
@Column(name = "MaxPrice", nullable = false, precision = 2)
public BigDecimal getMaxPrice() {
return maxPrice;
}
@Basic
@Column(name = "MinPrice", nullable = false, precision = 2)
public BigDecimal getMinPrice() {
return minPrice;
}
@Basic
@Column(name = "NumberOfTransaction", nullable = false)
public int getNumberOfTransaction() {
return numberOfTransaction;
}
@Basic
@Column(name = "PreviousClosingPrice", nullable = false, precision = 2)
public BigDecimal getPreviousClosingPrice() {
return previousClosingPrice;
}
@Basic
@Column(name = "StatusChgDate", nullable = false)
public Timestamp getStatusChgDate() {
return statusChgDate;
}
@Basic
@Column(name = "StatusChgUserID", nullable = false)
public int getStatusChgUserId() {
return statusChgUserId;
}
@Basic
@Column(name = "TradedShares", nullable = false)
public int getTradedShares() {
return tradedShares;
}
@ManyToOne
@JoinColumn(name = "StockId", referencedColumnName = "Id", nullable = false)
public Stock getStockByStockId() {
return stockByStockId;
}
... setters ...
}
StockPriceMast.java
@Entity
public class StockPriceMast {
private int id;
private Date entryDate;
private int entryUserId;
private String remarks;
private int status;
private Timestamp statusChgDate;
private int statusChgUserId;
private Date tranDate;
@Id
@Column(name = "Id")
public int getId() {
return id;
}
@Basic
@Column(name = "EntryDate")
public Date getEntryDate() {
return entryDate;
}
@Basic
@Column(name = "EntryUserID")
public int getEntryUserId() {
return entryUserId;
}
@Basic
@Column(name = "Remarks")
public String getRemarks() {
return remarks;
}
@Basic
@Column(name = "Status")
public int getStatus() {
return status;
}
@Basic
@Column(name = "StatusChgDate")
public Timestamp getStatusChgDate() {
return statusChgDate;
}
@Basic
@Column(name = "StatusChgUserID")
public int getStatusChgUserId() {
return statusChgUserId;
}
@Basic
@Column(name = "TranDate")
public Date getTranDate() {
return tranDate;
}
... setters ...
}
ここでクライアントはStockSymbol
と2つの日付を送信します。 01/01/2001から01/01/2002(Trandate on stockpricemast)のような特定の日付からmaxprice、minprice、closingprice、およびpreviousclosingPriceを設定する必要があります。
どのように私は休止状態を使用して行うことができますか? SQLクエリを達成したい。
Select d.MinPrice,d.MaxPrice,d.ClosingPrice, d.PreviousClosingPrice
from StockPriceDetl d
inner join Stock st on d.StockId=st.Id
inner join StockPriceMast sm on d.MastId = sm.Id
where st.StockSymbol='NABIL' and sm.TranDate
between '2001-01-01'and'2002-01-01'
StockPriceMastと他のテーブルとの関係はどうですか? –
StockDetlにはmastidという名前の外部キーがあり、StockDetlテーブルからminprice、maxprice、closingprice、およびprevisousclosingpriceをフェッチする必要があるStockPriceMastにトランザクション日付が格納されます。 – sagar
なぜStockDetlをStockPriceMastマッピングにハイバネート経由で追加しなかったのですか? – Maddy