hibernate-criteria

Regular expression with criteria

做~自己de王妃 提交于 2019-12-02 03:24:33
I have a table in which certain words or word groups are stored. I want to select entries which start with an uppercase letter, cointain no space and contain only letters. My SQL looks like this: select word from words where w_id > 100 AND word REGEXP '^[A-Z][A-Za-z]*$' limit 2000; How do I do the same thing using criteria? user3487063 Try this: List words = session.createCriteria(Word.class) .setProjection(Projections.property("word")) .add(Restrictions.and(Restrictions.gt("w_id",100), Restrictions.sqlRestriction(word REGEXP '^[A-Z][A-Za-z]*$'))) .setMaxResults(2000).list(); 来源: https:/

Hibernate hql/criteria result contains collection

左心房为你撑大大i 提交于 2019-12-02 02:44:13
I have two entities: public class Photo { Long id; String url; @ManyToOne @JoinColumn(name ="user_id") User user; // other fields and getters/setters } And second: public class User { Long id; @OneToMany(mappedBy = "user") private Collection<Photo> photos; // other fields and getters/setters } I am trying to get this DTO: public class UserDTO { Long id; List<String> photosUrls; } But I can't find the right solution. I wrote next criteria - find user with photos by login: getCurrentSession().createCriteria(User.class, "user") .createAlias("user.photos", "photos") .setProjection

Hibernate Criteria Query for multiple columns with IN clause and a subselect

丶灬走出姿态 提交于 2019-12-01 22:21:41
I have a question very similar to this question . I am selecting all data from table1 for all matching unique combinations of field3 and field4 from table2. Here is my stripped down SQL: select * from table1 as t1 where (t1.field1, t1.field2) in (select distinct field3, field4 from table2 as t2 where t2.id=12345); I need to translate my SQL to Hibernate Criteria. I have my entity objects mapping correctly to the tables and transform the response into the correct result entity, but I cannot get my where clause translated correctly. What I Have Criteria criteria = getSession().createCriteria

How to order result of hibernate based on a specific order

自古美人都是妖i 提交于 2019-12-01 15:39:29
I need to send a query to retrieve values that has a specific group of characters as following: Lets say I am interested in 'XX' so it should search for any field that its value starts with 'XX' or has ' XX' (space XX). For example XXCDEF , PD XXRF and CMKJIEK XX are valid results. I have following query that returns the correct results but I need to sort them in a way that it first return those with XX at the beginning then other results. As following: XXABCD XXPLER XXRFKF AB XXAB CD XXCD ZZ XXOI POLO XX Code Criteria criteria = session.createCriteria(Name.class, "name") .add(Restrictions

How to test Hibernate criteria queries without using any database?

痞子三分冷 提交于 2019-12-01 14:30:18
问题 I'm developing a Java application with lots of complex Hibernate criteria queries. I would like to test these criteria to make sure they are selecting the right, and only the right, objects. One approach to this, of course, is to set up an in-memory database (e.g. HSQL) and, in each test, make a round trip to that database using the criteria and then assert that the query results match my expectations. But I'm looking for a simpler solution, since Hibernate criteria are just a special kind of

Using sum and arithmetic result as order key in hibernate criteria

荒凉一梦 提交于 2019-12-01 14:27:14
How can I express this query in hibernate criteria. SELECT anId, SUM(fieldA) AS A, SUM(fieldB) AS B, SUM(fieldA)+SUM(fieldB) AS 'total' FROM tableA GROUP BY anId ORDER BY 'total' DESC LIMIT 5 following criteria should do the trick Criteria criteria = hibernateSession .createCriteria(YourEntity.class); criteria.setProjection(Projections .projectionList() .add(Projections.property("anId").as("prop1")) .add(Projections.sum("fieldA").as("prop2")) .add(Projections.sum("fieldB").as("prop3")) .add(Projections.sqlProjection( "sum(fieldA) + sum(fieldB) as total", new String[] { "total" }, new Type[] {

JPA Criteria Query - How to implement Join on two tables to get desired result in single Query

南楼画角 提交于 2019-12-01 13:54:24
I have 2 classes mapped with db Tables. Composite Primary Key Class : @Embeddable public class Pk implements Serializable, Cloneable { @Column(name = "dataId") private String dataId; @Column(name = "occurrenceTime") private Timestamp occurrenceTime; public String getDataId() { return dataId; } public Pk setDataId(String dataId) { this.dataId = dataId; return this; } public Timestamp getOccurrenceTime() { return occurrenceTime; } public Pk setOccurrenceTime(Timestamp occurrenceTime) { this.occurrenceTime = occurrenceTime; return this; } @Override public boolean equals(Object o) { if (this == o)

How to order result of hibernate based on a specific order

牧云@^-^@ 提交于 2019-12-01 13:44:21
问题 I need to send a query to retrieve values that has a specific group of characters as following: Lets say I am interested in 'XX' so it should search for any field that its value starts with 'XX' or has ' XX' (space XX). For example XXCDEF , PD XXRF and CMKJIEK XX are valid results. I have following query that returns the correct results but I need to sort them in a way that it first return those with XX at the beginning then other results. As following: XXABCD XXPLER XXRFKF AB XXAB CD XXCD ZZ

Using sum and arithmetic result as order key in hibernate criteria

一世执手 提交于 2019-12-01 13:12:33
问题 How can I express this query in hibernate criteria. SELECT anId, SUM(fieldA) AS A, SUM(fieldB) AS B, SUM(fieldA)+SUM(fieldB) AS 'total' FROM tableA GROUP BY anId ORDER BY 'total' DESC LIMIT 5 回答1: following criteria should do the trick Criteria criteria = hibernateSession .createCriteria(YourEntity.class); criteria.setProjection(Projections .projectionList() .add(Projections.property("anId").as("prop1")) .add(Projections.sum("fieldA").as("prop2")) .add(Projections.sum("fieldB").as("prop3"))

Hibernate Criteria: Left Outer Join with restrictions on both tables

陌路散爱 提交于 2019-12-01 03:59:16
I am doing a LEFT OUTER JOIN, but I am only able to apply Restrictions on the first table. Is there a way ti apply on the second table as well? Here is my code: Criteria criteria = this.crudService .initializeCriteria(Applicant.class).setFetchMode("products", FetchMode.JOIN);. This works (applicant has an applicantName property): criteria.add(Restrictions.eq("applicantName", "Markos") Neither of these works (product has a productName property) criteria.add(Restrictions.eq("productName", "product1") criteria.add(Restrictions.eq("products.productName", "product1") // products: the name of the