List of hibernation forecasts

I need to get only a few column values ​​from a table. So I used Projections to achieve this. The code works, but I do not think it is efficient.

My problem was that I used ProjectionsList and then set the .list criterion to ArrayList - the Bulletin object is null. I am not sure how to explain this. So I will put the code, and then please read below:

List<Bulletin> list = new ArrayList<Bulletin>(); BulletinList bulletinList = null; Criteria criteria = null; criteria = this.getSession().createCriteria(Bulletin.class) .setProjection(Projections.projectionList() .add(Projections.property(bulletinIdAttr)) .add(Projections.property(docNameAttr)) .add(Projections.property(docTypeCodeAttr)) ); criteria.addOrder(Order.desc(createdTimeAttr)); List<Object> rows = criteria.list(); for (Object r : rows) { Object[] row = (Object[]) r; Bulletin bull = new Bulletin(); bull.setBulletinId((Long) row[0]); bull.setDocumentName((String) row[1]); bull.setDocumentTypeCode((String) row[2]); list.add(bull); } bulletinList = new BulletinList(); bulletinList.setBulletins(list); return bulletinList; 

I just need to set the .list criteria in a BulletinList (a class containing a list of Bulletin objects). But when I use projections, the Bulletin object is null.

I also read another thread to use

 setResultTransformer(Transformers.aliasToBean 

But that doesn't work either. So can someone help with this on how to make the code better.

thanks

Harish

+4
source share
3 answers
  • Projections.property() takes a string as an argument. In the code you specified, the value of this string should be the name of a member of the Bulletin class. Presumably bulletinIdAttr , for example, is a String with that value, otherwise you will get runtime errors.
  • When you call setProjection on an instance of Criteria, you implicitly set ResultTransformer to PROJECTIONS , and that is what you want. No need to call setResultTransformer yourself. I would simplify the procedure a bit like this

     List<Object[]> rows = criteria.list(); for (Object[] row : rows) { Bulletin bull = new Bulletin(); bull.setBulletinId((Long) row[0]); bull.setDocumentName((String) row[1]); bull.setDocumentTypeCode((String) row[2]); list.add(bull); } 

But that should not matter to your results. Have you checked that rows empty?

+5
source

you can use

  criteria = this.getSession().createCriteria(Bulletin.class) .setProjection(Projections.projectionList() .add(Projections.property(bulletinIdAttr),"bulletinIdAttr") .add(Projections.property(docNameAttr),"docNameAttr") .add(Projections.property(docTypeCodeAttr),"docTypeCodeAttr") ); criteria.addOrder(Order.desc(createdTimeAttr)); criteria.setResultTransformer(new AliasToBeanResultTransformer(Bulletin.class)); List<Bulletin> bulletinList = criteria.list(); 

Here crit.setResultTransformer (the new AliasToBeanResultTransformer (Bulletin.class)) converts your result to the desired POJO class, but make sure your POJO class (Bulletin.class in your case) must have the appropriate setters to set property values.

Now the .list () criterion will return the POJO class of the newsletter list instead of Object.

+6
source

If you want to perform a second search using the same criteria, you must change or delete the projection. For example, if you first look for a counter:

 criteria.setProjection(Projections.rowCount()); Integer count = criteria.list().get(0); 

and then want to get all the objects:

 criteria.setProjection(null); List<Object> returnedObjects = criteria.list(); 
+3
source

All Articles