hibernate-criteria

How to specify pessimistic lock with Criteria API?

浪子不回头ぞ 提交于 2019-12-01 03:33:54
I am retrieving a list of objects in hibernate using Criteria API. However I need lock on those objects as another thread executing at the same time will get the exact objects and only one of the thread will succeed in absence of a pessimistic lock. I tried like below, but it is not working. List esns = session .createCriteria(Reddy_Pool.class) .add(Restrictions.eq("status", "AVAILABLE")) .add(Restrictions.eq("name", "REDDY2")) .addOrder(Order.asc("id")) .setMaxResults(n) .setLockMode(LockMode.PESSIMISTIC_WRITE) //not working at all .list(); Update : I am performing an update after this

How to retrieve a set of member objects using Hibernate?

青春壹個敷衍的年華 提交于 2019-12-01 02:17:56
This question is to follow up with my previous question . I need to retrieve a list of complex classes. Each has a few sets in it and just a specific number of them should be retrieved. I've already read answers of these questions 1 , 2 but none of them solved my issue. I need to find a list of students that are in a specific group and located in a specific location, and their phone numbers in their address. I also need to show distance of each student to a specific coordinate. Following code works fine, the only issue is I can not retrieve list of objects for example list of emails, list of

setResultTransformer in Criteria

感情迁移 提交于 2019-11-30 13:42:40
问题 What is the use of setResultTransformer method in criteria API? Can someone explain this with a simple example? I read the javadocs but i am not able to understand them clearly. Regards, 回答1: The default ResultTransformer for a Criteria query which does not use setProjections() will be ROOT_ENTITY . If we have Student in a ManyToMany relationship to Department a query might look like this ... Session session = (Session) getEntityManager().getDelegate(); Criteria crit = session.createCriteria

how to write Hibernate Criteria to take nested objects by Projection List?

China☆狼群 提交于 2019-11-30 13:01:50
问题 I want to take Nested object values in Hibernate Projection List. I having Pojo 'Charge' and 'Tariff' class with OneToMany and ManyToOne relations. My sample code is as following: Charge private String id; private Tariff tariff; private String name; @OneToMany(cascade= {CascadeType.ALL},fetch=FetchType.EAGER,mappedBy="charge") public Tariff getTariff() { return tariff; } public void setTariff(Tariff tariff) { this.tariff = tariff; } Tariff private String id; private String amount; private

Using Projections in JPA 2

断了今生、忘了曾经 提交于 2019-11-30 12:43:32
问题 I need to convert a Hibernate criteria query like the following curList = session.createCriteria(Islem.class) .createAlias("workingDay", "d") .setProjection(Projections.sum("amount")) .add(Restrictions.eq("currency", CURRENCY)) .add(Restrictions.eq("product", product)) .add(Restrictions.ne("status", INACTIVE)) .add(Restrictions.eq("d.status", ACTIVE)) .getResultList(); However in JPA (2) I have no idea how to implement the projection - in this case - the sum. It's odd that Hibernate and JPA

Hibernate Criteria Projection without mapped association of tables

こ雲淡風輕ζ 提交于 2019-11-30 10:22:57
I have 2 tables say Table1 and Table2 Now Table1 has 3 columns say t1, t2, t3 and Table2 has 2 columns t4 and t5. I have to fetch data from both tables by join but there is no mapped association between two tables in annotation or xml. Now main issue is I have to use hibernate projection to fetch selected columns from both tables say t1,t2 from Table1 and t4 from Table2. I have gone through the internet but have found examples of tables with association. Glad if there will be any guidance on this. Yes, this is supported in Hibernate. The only thing here is that we have to use HQL : 16.2. The

Hibernate Criteria Transformers.aliasToBean is not populating correct values

假如想象 提交于 2019-11-30 09:25:37
I am trying to create BO by joining my entity classes Criteria criteria = session.createCriteria(Report.class,"r"); criteria .createAlias("template", "t") .createAlias("constituents", "rc") .createAlias("rc.entity", "pe") .createAlias("pe.model", "m") .createAlias("pe.scenario", "s") .setProjection(Projections.projectionList() .add( Projections.property("r.Id")) .add( Projections.property("t.Typ")) .add( Projections.property("pe.bId")) .add( Projections.property("m.model")) .add( Projections.property("s.decay")) ).setMaxResults(100) .addOrder(Order.asc("r.Id")) .setResultTransformer

setResultTransformer in Criteria

跟風遠走 提交于 2019-11-30 08:15:37
What is the use of setResultTransformer method in criteria API? Can someone explain this with a simple example? I read the javadocs but i am not able to understand them clearly. Regards, The default ResultTransformer for a Criteria query which does not use setProjections() will be ROOT_ENTITY . If we have Student in a ManyToMany relationship to Department a query might look like this ... Session session = (Session) getEntityManager().getDelegate(); Criteria crit = session.createCriteria(Student.class) .createAlias('departments', 'department'); This query will return duplicates. But set the

How to retrieve a complex class and its members using Hibernate Projection?

为君一笑 提交于 2019-11-30 01:44:44
问题 I have a class as following that need to retrieve from DB using Hibernate. The problem is my class has multiple members and majority of them are classes, how can I retrieve them? @Entity public class Student { @Id long id; String name; String fname; @OneToMany List<Course> courses; @ManyToOne Dealer dealer; ... } @Entity public class Dealer { @Id long id; String name; @OneToMany(fetch = FetchType.LAZY, mappedBy = "cr.dealer", cascade = CascadeType.ALL) Set<Car> cars = new HashSet<Cars>(0); ..

Hibernate Criteria Projection without mapped association of tables

左心房为你撑大大i 提交于 2019-11-29 15:38:25
问题 I have 2 tables say Table1 and Table2 Now Table1 has 3 columns say t1, t2, t3 and Table2 has 2 columns t4 and t5. I have to fetch data from both tables by join but there is no mapped association between two tables in annotation or xml. Now main issue is I have to use hibernate projection to fetch selected columns from both tables say t1,t2 from Table1 and t4 from Table2. I have gone through the internet but have found examples of tables with association. Glad if there will be any guidance on