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();
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.