2017-02-17 10 views
0

私は外部キーで結合された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' 
+0

StockPriceMastと他のテーブルとの関係はどうですか? –

+0

StockDetlにはmastidという名前の外部キーがあり、StockDetlテーブルからminprice、maxprice、closingprice、およびprevisousclosingpriceをフェッチする必要があるStockPriceMastにトランザクション日付が格納されます。 – sagar

+0

なぜStockDetlをStockPriceMastマッピングにハイバネート経由で追加しなかったのですか? – Maddy

答えて

0

JPAリポジトリについて考えましたか? SQLを次のようなメソッドに定義できます。

public interface StockDetlRepository extends JpaRepository<StockDetl, Long>, QueryDslPredicateExecutor<StockDetl> { 

    @Query("SELECT sd from StockDetl sd, Stock s, StockPriceMast spm WHERE s.id = sd.stockId AND s.madtId = spm.id AND s.stockSymbol = ?1 AND spm.trandDate BETWEEN ?2 AND ?3") 
    List<StockDetl> findPricesByStockSymbolAndDate(String stockSymbol, Timestamp fromTranDate, Timestamp toTranDate); 

} 
+0

しかしStockDetlのmastIdは投稿されていません。 –

+0

私はこれについて知らなかった。私はこれを試してみたい。私のエンティティをどのように定義するべきか私にお勧めできますか?私はOnetoOneまたはOnetoManyまたはManytoOneをどこに置くべきかについて少し混乱します – sagar

+0

例はhttps://github.com/spring-projects/spring-petclinic –

関連する問題