简体   繁体   English

如何使用spring和hibernate从3个表中获取数据?

[英]how to fetch data from 3 tables by using spring and hibernate?

I have 3 tables which are joined by foreign key. 我有3个通过外键连接的表。 I need to fetch data by joining all the tables. 我需要通过加入所有表来获取数据。 Below are my 3 entity classes. 以下是我的3个实体类。

Stock.java 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 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 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 ...
}

Here client will send StockSymbol and two dates. 客户将在此处发送StockSymbol和两个日期。 i need to populate maxprice, minprice, closingprice and previousclosingPrice from certain dates like say 01/01/2001 to 01/01/2002 (Trandate on stockpricemast). 我需要从某些日期(例如2001年1月1日到2002年1月1日)填充maxprice,minprice,closingprice和previousclosesingPrice(股票价格桅杆上的日期)。

How can i do that using hibernate? 我该如何使用休眠模式? SQL query i want to achieve. 我想实现的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'

Have you thought about JPA-Repositories. 您是否考虑过JPA存储库。 There you can define your SQL to a method like: 在那里,您可以将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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Hibernate的多表中获取数据? - How to fetch data from multiples tables in Hibernate? 如何使用spring rest api从休眠的两个表中获取数据并在单个对象中返回URL - How to fetch data from two tables in hibernate with spring rest api and return in single object to url 如何使用 Hibernate 和 ManyToOne 映射从连接两个表的条​​件中获取数据? - How to fetch data from joining two tables with condition on both tables using Hibernate with ManyToOne mapping? 从4个表中获取数据使用休眠查询连接在一起 - Fetch data from 4 tables join together using hibernate query Hibernate-使用多对一注释从多个表中获取数据 - Hibernate - fetch data from multple tables using many to one annotation 如何使用 Spring Boot 的 JPA 存储库中的映射从 spring 引导中的多个表中获取数据 - How to fetch data from multiple tables in spring boot using mapping in Spring Boot's JPA repository 如何在 spring 中使用 Rest API 从多个表中获取数据 - how to fetch data using Rest API in spring boot from multiple tables 如何使用休眠从多表查询中获取数据? - How to fetch data from multiple table query using hibernate? JPA Hibernate SQL 查询不从关联表之一获取数据 - JPA Hibernate SQL Query to NOT fetch data from one of the associated tables 如何使用休眠从两个MySQL表中获取数据 - How to get data from two MySQL tables using hibernate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM