Hibernate select 查询具有不同的 where 子句
这可能很简单,但我在谷歌上花了几个小时,但找不到相关的答案。
我正在尝试将以下逻辑放入休眠状态,但我无法弄清楚如何进行。 这是我的设置: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
scroll()
(或iterate()
)方法而不是list()
滚动到查询结果方法。确保定期刷新和清除会话,如 批量更新的hibernate文档。
另外,内部查询至少应该有一个 where 子句,就像您的伪 SQL 查询一样。这两个实体可能应该与 OneToMany 或 OneToOne 列关联,这将完全避免对此内部查询的需要。
我认为最好的办法是阅读 Hibernate 文档。
You can scroll to the results of a query, using the
scroll()
(oriterate()
) method, rather than thelist()
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.
在 T1 表中迭代时,您可以运行一个包含 where 子句的查询,如下所示:
While Iterating in the T1 table, you can run a query that will have a where clause in it like this: