hibernate-criteria

How to make HIbernate fetch all properties of root entity and only specific properties of associated entity?

我的梦境 提交于 2019-12-20 10:47:08
问题 I have root entity Hostel and its single association User owner . When I fetch Hostel entity I need to eagerly fetch User owner , but only owner 's 3 properties : userId,firstName,lastName. For now my criteria query is : Criteria criteria = currenSession().createCriteria(Hostel.class); criteria.add(Restrictions.ge("endDate", Calendar.getInstance())); if (StringUtils.notNullAndEmpty(country)) { criteria.add(Restrictions.eq("country", country)); } Long count = (Long) criteria

How to use Hibernate Criteria objects for multiple and/or conditions

守給你的承諾、 提交于 2019-12-20 08:44:59
问题 I need to create a Hibernate criteria restriction that ors 3 Conditions. The problem is that the last condition is acutally to conditions using the AND operator. My first condition: Criterion startInRange = Restrictions.between("expectedStartCanonicDate", rangeStart, rangeEnd); My second condition: Criterion endInRange = Restrictions.between("expectedCompletionCanonicDate", rangeStart, rangeEnd); MY third condition needs to AND the following two conditions together: criteria.add(Restrictions

Hibernate Criteria Return parent record that have one-to-one child record not null?

心已入冬 提交于 2019-12-20 06:39:51
问题 I have a one-to-one relation like this The Parent @JsonAutoDetect @Entity @Table(name = "Parent") public class Parent{ private Integer id; private Child child; @Id @GeneratedValue(strategy= GenerationType.AUTO) @Column (name="idparent") public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parent") @JoinColumn(name="idchild", nullable=true) public Child getChild() { return child; } public

One to Many search using AND condition

吃可爱长大的小学妹 提交于 2019-12-20 05:02:34
问题 I have the following product which contain many colors. I wish to find the product which contain at least RED and GREEN. Product class String id; List<Color> colors{}; Color class id color kindly ignore the syntax error. I'm able to use the following to search OR condition. Criteria criteria = createCriteria(); criteria.createAlias("colors","colors"); List<String> colorsList = new LinkedList(); colorsList.add("GREEN"); colorsList.add("RED"); criteria.add(Restriction.in("colors.color"

One to Many search using AND condition

↘锁芯ラ 提交于 2019-12-20 05:01:22
问题 I have the following product which contain many colors. I wish to find the product which contain at least RED and GREEN. Product class String id; List<Color> colors{}; Color class id color kindly ignore the syntax error. I'm able to use the following to search OR condition. Criteria criteria = createCriteria(); criteria.createAlias("colors","colors"); List<String> colorsList = new LinkedList(); colorsList.add("GREEN"); colorsList.add("RED"); criteria.add(Restriction.in("colors.color"

Regular expression with criteria

假装没事ソ 提交于 2019-12-20 03:38:10
问题 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? 回答1: Try this: List words = session.createCriteria(Word.class) .setProjection(Projections.property("word")) .add(Restrictions.and(Restrictions.gt("w_id",100), Restrictions

Hibernate criteria api 'Select in'

梦想的初衷 提交于 2019-12-19 06:17:23
问题 is it possible to create a 'select in'-query with the hibernate critiria api ? Example : I have two tables in a 1:n relation, company and department select * from company c where c.id in (select company_id from department d where d.departmentname = 'HR' and d.location = 'xyz') 回答1: You can use for this DetachedCriteria DetachedCriteria subCriteria= DetachedCriteria.forClass(Departament.class); subCriteria.add(Property.forName("departmentname ").eq("HR")); subCriteria.add(Property.forName(

How to specify pessimistic lock with Criteria API?

旧巷老猫 提交于 2019-12-19 05:50:58
问题 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

How to retrieve a set of member objects using Hibernate?

爱⌒轻易说出口 提交于 2019-12-19 05:06:58
问题 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

Hibernate, getting duplicate values

强颜欢笑 提交于 2019-12-19 03:12:08
问题 I am writing a very simple query, but I am getting duplicate values for some reason. Criteria cr = session.createCriteria(ProcessInstance.class, "p") .add(Restrictions.isNull("end")); @Cleanup ScrollableResults sr = cr.scroll(ScrollMode.FORWARD_ONLY); while (sr.next()) { pi = (ProcessInstance) sr.get(0); String id = pi.getId(); //Getting duplicate values } The pi.getId() returns duplicate values. ie: *9,9,10,10,11,11 etc* However, running this query directly in mysql SELECT * FROM JBPM