Hibernate select 查询具有不同的 where 子句

发布于 2025-01-07 17:09:49 字数 1324 浏览 0 评论 0原文

这可能很简单,但我在谷歌上花了几个小时,但找不到相关的答案。

我正在尝试将以下逻辑放入休眠状态,但我无法弄清楚如何进行。 这是我的设置:Mysql 数据库有 2 个表 T1(ID,C1,C2) 和 T2(column1,column2,column3)。 这就是我想要实现的

    results = select ID, from T1 //This will result in multiple rows.

    for(eachRow in results )
    {
    select column1,column2,column3 from T2 where ID=eachRow.ID
    //Do some computation like
    //Assign value of table "T1" column "C1" to table "T2" column "column1"
    column1 = eachRow.C1

    }

现在我可以想到使用 Hibernate“from”子句将整个表读取到列表中并迭代它的逻辑。但是,这可能会导致 OutOfMemory 异常。您能否建议 HQL 必须如何?

这就是我所做的

Session session = HibernateSession.getSessionFactory().openSession();
        Transaction aTableTx = null;
        try {
            aTableTx = session.beginTransaction();

            _allRows = session.createQuery("from T1").list();

            for (Iterator<T1> iterator = _allRows.iterator(); iterator.hasNext();)
            {
                T1 aRow = iterator.next();
                //Here I am planning to run a similar to
                            //_allRows = session.createQuery("from T2").list();
                            //Store the result and iterate through the list, but 
                            //it is very inefficient.
            }

            aTableTx.commit();

This might be quite simple but I have spent hours in Google but couldn't find a relevant answer.

I am trying to put the below logic into hibernate but i am not able to figure out how.
This is my setup: Mysql database with 2 tables T1(ID,C1,C2) and T2(column1,column2,column3).
This is what I want to implement

    results = select ID, from T1 //This will result in multiple rows.

    for(eachRow in results )
    {
    select column1,column2,column3 from T2 where ID=eachRow.ID
    //Do some computation like
    //Assign value of table "T1" column "C1" to table "T2" column "column1"
    column1 = eachRow.C1

    }

For now I could think of a logic of reading the entire table using the Hibernate "from" clause into a List and iterating through it. However this might cause a OutOfMemory exception. Could you please suggest how the HQL must be?

This is what I did

Session session = HibernateSession.getSessionFactory().openSession();
        Transaction aTableTx = null;
        try {
            aTableTx = session.beginTransaction();

            _allRows = session.createQuery("from T1").list();

            for (Iterator<T1> iterator = _allRows.iterator(); iterator.hasNext();)
            {
                T1 aRow = iterator.next();
                //Here I am planning to run a similar to
                            //_allRows = session.createQuery("from T2").list();
                            //Store the result and iterate through the list, but 
                            //it is very inefficient.
            }

            aTableTx.commit();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半透明的墙 2025-01-14 17:09:49

您可以使用 scroll()(或 iterate())方法而不是 list() 滚动到查询结果方法。

确保定期刷新和清除会话,如 批量更新的hibernate文档

另外,内部查询至少应该有一个 where 子句,就像您的伪 SQL 查询一样。这两个实体可能应该与 OneToMany 或 OneToOne 列关联,这将完全避免对此内部查询的需要。

我认为最好的办法是阅读 Hibernate 文档。

You can scroll to the results of a query, using the scroll() (or iterate()) method, rather than the list() method.

Make sure to regularly flush and clear the session, as explained in the hibernate documentation of batch updates.

Also, the inner query should at least have a where clause, as your pseudo-SQL query has one. The two entities should probably be associated with OneToMany or OneToOne column, which would avoid the need for this inner query completely.

I think the best thing to do would be to read the Hibernate documentation.

浅笑轻吟梦一曲 2025-01-14 17:09:49

在 T1 表中迭代时,您可以运行一个包含 where 子句的查询,如下所示:

T1 aRow = iterator.next();
Query query= sessionFactory.getCurrentSession().createQuery("from T2 as where T2.ID=:aRowID");
query.setInteger("aRowID", aRow.ID);

While Iterating in the T1 table, you can run a query that will have a where clause in it like this:

T1 aRow = iterator.next();
Query query= sessionFactory.getCurrentSession().createQuery("from T2 as where T2.ID=:aRowID");
query.setInteger("aRowID", aRow.ID);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文