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
Related
I want to build a query like below using Hibernate Projections attribute. Can someone check the below. I have written java code like.
DetachedCriteria dCriteria = DetachedCriteria.forClass(FinancialYearQuater.class, "FinancialYearQuater");
dCriteria.add(Restrictions.eq("FinancialYearQuater.finYear", year));
dCriteria.addOrder(Order.asc("finYear"));
dCriteria.setResultTransformer(Projections.distinct(Projections.property("id")));
List<FinancialYearQuater> list = (List<FinancialYearQuater>) findAll(dCriteria);
Here's the SQL query:
select
distinct
this_.FINY_NAME,
this_.FINY_YEAR,
this_.QTR_NAME,
this_.QTR_NO,
this_.QTR_PERIOD
from
V_FINYR_QTR this_
where
this_.FINY_YEAR=2016
order by
this_.FINY_YEAR asc
I have written below code. Is that the correct way get the distinct data?
DetachedCriteria dCriteria = DetachedCriteria.forClass(FinancialYearQuater.class, "FinancialYearQuater");
dCriteria.add(Restrictions.eq("FinancialYearQuater.finYear", year));
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("FinancialYearQuater.finYear"), "finYear");
projList.add(Projections.property("FinancialYearQuater.finYearName"), "finYearName");
projList.add(Projections.property("FinancialYearQuater.qtrNo"), "qtrNo");
projList.add(Projections.property("FinancialYearQuater.qtrPeriod"), "qtrPeriod");
dCriteria.setProjection(Projections.distinct(projList));
dCriteria.addOrder(Order.asc("finYear"));
List<FinancialYearQuater> list = (List<FinancialYearQuater>) findAll(dCriteria);
How do i acheive the following SQL Query using Hibernate Criteria Query:
select * from Table1 where (A,B) in (select A,B from Table2)
Assuming we have Criteria for Table 1 and Detached Criteria for Table 2. This below code work flawlessly:
Criteria criteria = new Criteria(Table1.class);
DetachedCriteria dc = DetachedCriteria.forClass(Table2.class);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("column1"));
projList.add(Projections.property("column2"));
dc.setProjection(Projections.distinct(projList));
criteria.add(Subqueries.propertiesIn(new String[]{"column1","column2"}, dc));
I have an entity PrintingFare
and a foreign key there - Printer,
so how do I use Criteria API that way so I get a set of Printers, which have Printing fares?
Basically I have a class PrintingFare with this field
#ManyToOne
#JoinColumn(name = "printer_id", foreignKey = #ForeignKey(name = "fk_fares_ref_printer_id"))
private Printer printer;
How do I get a set of Printers using Criteria API?
I haven't tested this, but you can try:
Criteria criteria = getSession().createCriteria(PrintingFare.class);
criteria.setProjection(Projections.distinct(Projections.property("printer"));
List<Printer> list = criteria.list();
Let me know if it works. It will probably have to be tweaked.
You should add restrictions to the query
Criteria criteria = getSession().createCriteria(PrintingFare.class);
criteria.add(Restrictions.eq("printer.id", printerId));
List<PrintingFare> list = criteria.list();
Criteria criteria = getSession().createCriteria(Printer.class);
criteria.createAlias("printerFairs", "pf").add(Restrictions.in("pf.id", printerFairIds));
List<Printer> list = criteria.list();
If PrinterFares is mapped from the Printer.class side ...
Criteria criteria = getSession().createCriteria(Printer.class);
criteria.createAlias("printerFares", "pf");
criteria.add(Restrictions.isNotEmpty("pf"));
crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
List<Printer> printers = criteria.list();
If you are unable to change the mapping of Printer.class ...
Criteria criteria = getSession().createCriteria(Printer.class);
// create a subquery
DetachedCriteria dc = DetachedCriteria.forClass(PrinterFare.class);
dc.setProjection(Projections.property("printer.id"));
// restrict by subquery
criteria.add(Subqueries.in("id", dc);
crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
List<Printer> printers = criteria.list();
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.
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));