hibernate Projections for spring boot application - java

i have a table which contains following fields
|id|feedback_status|financial_year|attendence|behaviour|total_feedback|user_id|total_behaviour|
Iam fetching feedback_status and total_feedback from that table and return as json data using hibernate with projections and projectionlist.
List<StudentFeedback> results = null;
Session session = getSession();
Criteria criteria = session.createCriteria(StudentFeedback.class);
ProjectionList projlist = Projections.projectionList();
projlist.add(Projections.property("totalFeedback").as("qty"));
projlist.add(Projections.groupProperty("feedbackStatus").as("feedName"));
criteria.setProjection(projlist);
criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
results = criteria.list();
}
return GsonUtils.toGson(results);
}
My feedback_status and total_feedback contains following values
|feedback_status| |total_feedback |
Excellent 23
Average 11
Poor 12
Good 13
Poor 12
So the problem is whenever returning the json,it prints the feedback_status value poor two times and i want only the values to be printed once and if it occurs more than once then sum the total_feedback corresponding to the feedback_statusand returns to single one.

I got the answer by using groupProperty in hibernate porjections
By the way Thanks Pradeep CG
List<StudentFeedback> results = null;
Session session = getSession();
Criteria criteria = session.createCriteria(StudentFeedback.class);
ProjectionList projlist = Projections.projectionList();
projlist.add(Projections.sum("totalFeedback").as("qty"));
projlist.add(Projections.groupProperty("feedbackStatus").as("feedName"));
criteria.setProjection(projlist);
criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
results = criteria.list();
return GsonUtils.toGson(results);

Related

converting inner query using Hibernate Criteria

I'm trying to convert below nested query into Hibernate Criteria, but not able to do.
Actually, trying to count and sum the rows from the result set.
so anybody have any ideas ?
Thanks in advance.
SELECT DISTINCT host_platform,
host_manufacturer,
COUNT(phy_1),
COUNT (vir_1),
SUM (server_cost) AS server_cost,
SUM (book_value) AS book_value,
SUM (maintenance_cost) AS main_cost,
SUM (current_cost) AS curr_cost
FROM (SELECT DISTINCT host_platform,
host_manufacturer,
phy_1,
vir_1,
server_cost,
book_value,
maintenance_cost,
current_cost
FROM tf_server_list_v
where host_platform = 'UNIX')
--ipaddress = '142.125.21.70')
GROUP BY host_platform, host_manufacturer;
Here below is the criteria written for above query,
For select main query,
Criteria mainCriteria = getSession().createCriteria(TfServerListNew.Class);
ProjectionList projOSList1 = Projections.projectionList();
projOSList1.add(Projections.property("hostPlatform"), "hostPlatform");
ProjectionList projectionList1 = Projections.projectionList();
projectionList1.add(Projections.distinct(projOSList));
projectionList1.add(Projections.alias(Projections.property("hostManufacturer"), "hostManufacturer"));
projectionList1.add(Projections.alias(Projections.count("physicalFlag"), "physical"));
projectionList1.add(Projections.alias(Projections.count("virtualFlag"), "virtual"));
projectionList1.add(Projections.alias(Projections.sum("serverCost"), "serverCost"));
projectionList1.add(Projections.alias(Projections.sum("bookValue"), "bookValue"));
projectionList1.add(Projections.alias(Projections.sum("mainCost"), "mainCost"));
projectionList1.add(Projections.alias(Projections.sum("currCost"), "currCost"));
mainCriteria.setProjection(projectionList1);
For inner query to populate result set and pass to main query,
final DetachedCriteria criteria = DetachedCriteria.forClass(TfServerListNew.class);
ProjectionList projOSList = Projections.projectionList();
projOSList.add(Projections.property("hostPlatform"), "hostPlatform");
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.distinct(projOSList));
projectionList.add(Projections.alias(Projections.property("hostManufacturer"), "hostManufacturer"));
projectionList.add(Projections.alias(Projections.property("physicalFlag"), "physical"));
projectionList.add(Projections.alias(Projections.property("virtualFlag"), "virtual"));
projectionList.add(Projections.alias(Projections.property("serverCost"), "serverCost"));
projectionList.add(Projections.alias(Projections.property("bookValue"), "bookValue"));
projectionList.add(Projections.alias(Projections.property("mainCost"), "mainCost"));
projectionList.add(Projections.alias(Projections.property("currCost"), "currCost"));
But how to pass this subquery to main query is not getting.
Use
mainCriteria.add(Restrictions.sqlRestriction("your nested query"));
or you can use other versions of sqlRestriction based on requirement
I think This method is good one.Or as you have not provided details no answer related to your code.

Self-Join using Hibernate Criteria

I'm trying to reproduce a SQL query containing self-joins using Hibernate. This is the SQL:
select cur.*
from first ft1,
first ft2,
second sc1,
second sc2,
third thd
where sc1.id = sc2.id
and sc1.idFirst = ft1.id
and sc2.idFirst = ft2.id
and ft1.is <> ft2.id
and ft1.id = thd.idFirst
and ft2.id = ?
I've tried using Criteria and DetachedCriteria as follows, by I'm not able to get it to work:
Criteria criteria = this.getSession().createCriteria(Third.class);
DetachedCriteria first1Criteria = DetachedCriteria.forClass(First.class);
first1Criteria.setProjection(Projections.property("id"));
DetachedCriteria first2Criteria = DetachedCriteria.forClass(First.class);
first2Criteria.setProjection(Projections.property("id"));
first2Criteria.add(Restrictions.eq("id", id)); // where id is passed in from the calling method
first2Criteria.getExecutableCriteria(getSession()).list();
DetachedCriteria second1Criteria = DetachedCriteria.forClass(Second.class);
second1Criteria.setProjection(Projections.property("id"));
DetachedCriteria second2Criteria = DetachedCriteria.forClass(Second.class);
second2Criteria.setProjection(Projections.property("id"));
second1Criteria.add(Property.forName("id.id").in(second2Criteria ));
second1Criteria.add(Property.forName("id.idFirst").in(first1Criteria));
second1Criteria.add(Property.forName("id.idFirst").in(first2Criteria));
first1Criteria.add(Property.forName("id").notIn(first2Criteria));
criteria.add(Property.forName("id").in(first1Criteria));
return criteria.list();
No exceptions are thrown in this case, but the query isn't actually executed. I've tried a few differnt combinations along these lines, but unsuccessfully. Any help is much apreciated!

Converting the following criteria into SQL

I have the following criteria that I am using,..
List<boop> books =null;
Criteria criteria = session.createCriteria(boop.class);
ProjectionList proList = Projections.projectionList();
proList.add(Projections.property("AAA"));
proList.add(Projections.property("BBB"));
RETURN criteria.list();
please advise how can I convert it in HQL..
Have a look at the Projections javadoc. Each Projections property corresponds to a database field.
select b.AAA, b.BBB
from boop b

How to join tables using hibernate criteria

I am trying to join multiple table to join using criteria but getting error in doing so can someone please help me in it
My code is
final Session session = getSession();
final Criteria criteria = session.createCriteria(ReferralPaymentInfo.class).createCriteria("SIGNUP_REFERRAL");
System.out.println("before");
List list = criteria.list();
System.out.println("after");
I also tried this code
final Session session = getSession();
final Criteria criteria =session.createCriteria(ReferralPaymentInfo.class);
criteria.setFetchMode("SIGNUP_REFERRAL", FetchMode.JOIN);
List list = criteria.list();
This gives result only from table ReferralPaymentInfo and not considering table SIGNUP_REFERRAL
Can some one please help me out
T
try this
DetachedCriteria ownerCriteria = DetachedCriteria.forClass(Owner.class);
ownerCriteria.setProjection(Property.forName("id"));
ownerCriteria.add(Restrictions.eq("ownername", "name"));
Criteria criteria = getSession().createCriteria(Pet.class);
criteria.add(Property.forName("ownerId").in(ownerCriteria));

hibernate groupby

I want to know how to make groupby in hibernate.
my code is below but it is not working please tell me how do this :
Session session = getSession();
Criteria c = session.createCriteria(CardLog.class);
c.add(Restrictions.ge(CARD_PUNCHING_TIME, todaysDate));
c.setProjection(Projections.groupProperty(USER_ID));
c.addOrder(Order.desc(USER_ID));
cardLogList = c.list();
System.out.println("----------------" + cardLogList);
i think you have to use like this
Session session = getSession();
Criteria c = session.createCriteria(CardLog.class);
c.add(Restrictions.ge("bean property todaydate", todaysDate));
c.setProjection(Projections.groupProperty("bean property userId"));
c.addOrder(Order.desc("bean property userId"));
cardLogList = c.list();
System.out.println("----------------" + cardLogList);

Categories