0

I have the below hibernate native query as shown below the idea is not to use the native query and switch to hibernate criteria api

 <![CDATA[select count(iilnmp.INV_LINE_NOTE_ID) from IOA_INV_LINE_NOTE_MAP iilnmp , 
                                                    IOA_INVOICE_LINE_NOTES iiln , IOA_INVOICE_LINE iil
                                                   where   iilnmp.INV_LINE_NOTE_ID = iiln.ID and iiln.INLI_ID =iil.id and iil.ID = ?]]>

which i am calling from a method as shown below now my query is that instead of having native query can i use criteria also to achieve the same result

 public int findAttachementsCount(long id)
    {   
        Query query = session.getNamedQuery("attachmentQuery");
        query.setParameter(0, id);
        int attachCount = query.list().size(); 
        return attachCount;
    }   

Folks please advise for this. Can somebody please look this into priority..!!

dgfd hgfhg
  • 105
  • 1
  • 8

2 Answers2

0
public int findAttachementsCount(long id)
    Criteria c = session.createCriteria(YourClass.class, "alias");
    c.createAlias("fieldOfyoursecondClass.role", "role"); // inner join by default
    c.setProjection(Projections.rowCount())
    c.add(Restrictions.eq("alias.id", id));
return ((Long)c.uniqueResult()).getIntValue();

Original question - Joins | Original question - Source - Count

Community
  • 1
  • 1
pedroct92
  • 404
  • 1
  • 4
  • 18
0
Criteria c = session.createCriteria(IOA_INV_LINE_NOTE_MAP.class, "iilnmp");
c.createAlias("iilnmp.INV_LINE_NOTE_ID", "iiln");
c.createAlias("iiln.INLI_ID", "iil");

// restrictions
c.add(Restrictions.eq("iil.ID", id));

// projections
c.setProjections(Projections.count("iilnmp.INV_LINE_NOTE_ID"));

// run query
c.uniqueResult();
user3206236
  • 315
  • 5
  • 14