0

Hi I have created many to one relationship in hibernate. Following is the code for that.

there are thousands of records present in B table which is link to single record of table A. When i used getBList() method it will returns thousands of record and JAVA goes OUT OF MEMORY. So how can i solve this problem.

@Entity
@Table(name = "A")
public class A {

    private int Id;
    private String aName;
    private List<MksReleaseInfo> bList;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    public int getId() {
        return releaseId;
    }

    public void setId(final int Id) {
        this.Id = Id;
    }

    @Column(name = "aname", unique = true)
    public String getAName() {
        return aName;
    }

    public void setAName(final String aName) {
        this.aName = aName;
    }
    @OneToMany(mappedBy = "aName")
    public List<MksReleaseInfo> getBList() {
        return bList;
    }

    public void setBList(final List<B> bList) {
        this.bList = bList;
    }
}   


@Entity
@Table(name = "B")
public class B {

    private int bIndex;
    private int bpriority;
    private A aName;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    protected int getBIndex() {
        return mksReleaseInfoIndex;
    }

    protected void setBIndex(final int bIndex) {
        this.bIndex = bIndex;
    }

    @Column(name = "priority")
    public int getBPriority() {
        return bpriority;
    }

    public void setBPriority(final int bpriority) {
        this.bpriority = bpriority;
    }

    @ManyToOne
    @JoinColumn(name = "Id")
    public A getAName() {
        return aName;
    }

    public void setAName(final A aName) {
        this.aName = aName;
    }
}

after all the comments i have implemented the following code. but again it gives OUT OF MEMORY. Should i have to flush the memory explicitly and how?

public List<B> getList(String name, int offset, int limit) throws DAOException {
        try {
            String hql = "from B where name = :name";
            begin();
            Query query = getSession().createQuery(hql);
            query.setString("name", name);

            if(offset > 0){
                query.setFirstResult(offset);
            }

            if(limit > 0){
                query.setMaxResults(limit);
                query.setFetchSize(limit);
            }
            commit();
            return query.list();
        } catch (HibernateException e) {
            rollback(); 
        }
    }

    public Long countB(String name) throws DAOException {
        try {
            String hql = "select count(*) from B where name = :name";
            begin();
            Query query = getSession().createQuery(hql);
            query.setString("name", name);
            commit();
            return (Long)query.uniqueResult();
        } catch (HibernateException e) {
            rollback();
        }
    }


    long count = countB(name);
    int counter = (int) (count / 200);
    if(count%200 > 0){
        counter++;
    }
    for(int j = 0;j<counter;j++){
        lists = getList(name, j*200, 200);

        for(B count1 : lists){
            System.out.println(count1);
        }
    }
4

4 回答 4

1

您可以引入 aDAO以便以分页方式从B给定 对象中检索记录。A

例如:

public interface BDao {

   Page findByA(A a, PageRequest pageRequest);

}

也许您可以从Spring Data中采用的方法中获得一个想法

于 2012-07-06T06:37:06.000 回答
1

设置MaxResults数据源的属性,它将限制您获取的记录数。

此外,您可以使用-Xmx256m. 这会将最大堆分配大小设置为 256MB。您可以根据需要进行设置。

于 2012-07-06T06:37:25.863 回答
1

为此,您可以使用带分页的查询。在Query课堂上,您可以找到可以帮助您遍历记录的方法setFirstResultsetMaxResults如果您需要加载所有 B 对象并存储它们,您可以通过设置-Xmx选项调整 java 的内存设置。您也可以尝试声明某种简化的 B 类(例如ReducedB),它只包含必填字段,并使用带有转换的迭代BReducedB减少内存使用。

你也可以检查这个问题。我认为它与您想要的很接近。

PS 最终解决方案将取决于您要解决的特定问题。

于 2012-07-06T08:52:43.237 回答
0

我遇到过同样的问题。我查看了我的代码和服务器空间,但没有任何帮助。后来我查看了数据并意识到错误放置的数据正在使应用程序使用大量的处理能力。确保您在子类中没有重复数据。

于 2018-11-09T19:37:44.720 回答