【问题标题】:Huge data being fetch using Hibernate使用 Hibernate 获取大量数据
【发布时间】:2015-08-19 10:18:47
【问题描述】:

从我的数据库中获取数据时遇到问题,大约有 600 万条记录。这是一个 Web 应用程序,然后 tomcat 日志返回超过 GC 限制。

其他解决方案是分页然后我们查询的每个页面。

也许还有其他一些技术/解决方法。

它有 2 个联合列,它们也是对象。

public class PricesRaw {

@Id
@GeneratedValue
@Column(name = "price_raw_id")
private int price_raw_id;

@Column(name = "date")
private Date date;

@Column(name = "year")
private int year;

@Column(name = "size")
private int size;

@Column(name = "stock")
private int stock;

@Column(name = "price")
private double price;

@Column(name = "idccy")
private int ccy;

@Column(name = "status")
private int status;

@ManyToOne
@JoinColumn(name = "idwine")
private Wine wine;

@ManyToOne
@JoinColumn(name = "idmerchant")
private Merchant merchant;

public PricesRaw(){}

public PricesRaw(Date date, int year, int size, int stock, Double price,
                 int idccy, int status, Wine idWine, Merchant idMerchant)
{
    this.date = date;
    this.year = year;
    this.size = size;
    this.stock = stock;
    this.price = price;
    this.ccy = idccy;
    this.status = status;
    this.wine = idWine;
    this.merchant = idMerchant;
}   

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public int getYear() {
    return year;
}

public void setYear(int year) {
    this.year = year;
}

public int getSize() {
    return size;
}

public void setSize(int size) {
    this.size = size;
}

public int getStock() {
    return stock;
}

public void setStock(int stock) {
    this.stock = stock;
}

public double getPrice() {
    return price;
}

public void setPrice(double price) {
    this.price = price;
}

public int getCcy() {
    return ccy;
}

public void setCcy(int ccy) {
    this.ccy = ccy;
}

public int getStatus() {
    return status;
}

public void setStatus(int status) {
    this.status = status;
}

public Wine getWine() {
    return wine;
}

public void setWine(Wine wine) {
    this.wine = wine;
}

public Merchant getMerchant() {
    return merchant;
}

public void setMerchant(Merchant merchant) {
    this.merchant = merchant;
}   

} 这是我的休眠被调用。我是 Hibernate 的新手,仍在探索它的功能,任何技术都将不胜感激

    public List<PricesRaw> getPrices()
{
    Criteria criteria = getSession().createCriteria(PricesRaw.class);
    // Problem is here. 
    return (List<PricesRaw>) criteria.list();
}

【问题讨论】:

    标签: database hibernate orm criteria


    【解决方案1】:

    是的,Saul 我们在实时应用程序中面临这种问题。但就技术而言,我们有两种方法可以完成。

    1. 延迟加载 / 分页
    2. 渴望加载

    延迟加载

    要实现延迟加载,请使用以下代码。

     public List<PricesRaw> getPrices() {
        Criteria criteria = getSession().createCriteria(PricesRaw.class);
        criteria.setFirstResult(first);
        criteria.setMaxResults(pageSize);
        return (List<PricesRaw>) criteria.list();
    }
    

    这样您可以避免GC Limit exceeded 以及tomcat 内存不足。

    希望对您有所帮助!

    【讨论】:

    • 我可以想象你的想法。您能否进一步解释一下您对 first 和 pageSize 的含义?感谢您的回复。
    • 它实际上是你的页面上下限。假设你想显示前 10 条记录,那么 first = 1pageSize = 10
    猜你喜欢
    • 2017-10-01
    • 2019-02-05
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多