0

I am required to use StatelessSession because of the memory consumption when handling millions of rows and the method createQuery from sessions only accepts String.

The code looks like this:

StatelessSession session = ((Session) EntityManager
                    .getDelegate()).getSessionFactory().openStatelessSession();
                    
CriteriaBuilder criteriaBuilder = EntityManager
                    .getCriteriaBuilder();

CriteriaQuery<Concepts> queryConcepts = criteriaBuilder
                    .createQuery(Concepts.class);

Root<Concepts> rootConcepts = queryConcepts
                    .from(Concepts.class);
Root<TecAccountingExportTemp> rootExportTemp = queryConcepts
                    .from(ExportTemp.class);

rootConcepts.alias("a");
rootExportTemp.alias("b");

queryConcepts
         .select(rootConcepts);

Predicate fieldIdCab = criteriaBuilder
          .equal(rootConcepts
          .get(Concepts.XPTO_NAME),
                            rootExportTemp
                                    .get(ExportTemp.XPTO_NAME));

Predicate fieldKey = criteriaBuilder.equal(
                    rootExportTemp
                            .get(ExportTemp.KEY_NAME),
                    ExportTmpType.CONCEPTS.getValue());

Predicate fieldExportId = criteriaBuilder.equal(
                    rootExportTemp
                            .get(ExportTemp.ID_NAME),
                    tecAccountingExport.getId());

Predicate predicateFilter = criteriaBuilder.and(fieldIdCab,
                    fieldKey, fieldExportId);

queryConcepts.where(predicateFilter);
            
TypedQuery<Concepts> query = EntityManager
                    .createQuery(queryConcepts);
                    
                    
Query scrollQuery = query.unwrap(org.hibernate.Query.class);
                    

scrollconcepts = session
                    .createQuery(
                            scrollQuery.getQueryString()).setReadOnly(true)
                    .setFetchSize(maxCachedRecords).setCacheable(false)
                    .scroll(ScrollMode.FORWARD_ONLY);

The problem is that when the parameters are an instance of String, the scrollQuery.getQueryString() doesn't place the variable values correctly, they are like (xpto=:param1...) instead of what happens when the parameters are an instance of Integer (xpto=1 - correctly formating param1 as 1).

Can someone help me?

  • `method createQuery from sessions only accepts String` what hibernate version are you using? It should definitely accept `CriteriaQuery` directly as well, leaving you with `session.createQuery(queryConcepts)...` – XtremeBaumer Jun 09 '22 at 14:46
  • I am using hibernate-core 4.2.0 – Hugo Marinho Jun 09 '22 at 14:58
  • Any reason to use a version thats 9 years old? You will have an easier time using an up to date version – XtremeBaumer Jun 10 '22 at 06:29
  • Yes I understand that a new version will resolve my problem but right now I can't do update for a new version of Hibernate – Hugo Marinho Jun 17 '22 at 10:26

0 Answers0