criteria

Hibernate detached queries as a part of the criteria query

走远了吗. 提交于 2019-12-03 05:20:09
问题 java experts can you please help me write detached queries as a part of the criteria query for the following SQL statement. select A.* FROM AETABLE A where not exists ( select entryid FROM AETABLE B where B.classpk = A.classpk and B.userid = A.userid and B.modifiedDate > A.modifiedDate ) and userid = 10146 回答1: You need to write a correlated subquery. Assuming property / class names match column / table names above: DetachedCriteria subquery = DetachedCriteria.forClass(AETable.class, "b")

Hibernate criteria query on different properties of different objects

旧城冷巷雨未停 提交于 2019-12-03 05:06:33
问题 Suppose I have classes like: class A { B getB(); C getC(); } class B { String getFoo(); } class C { int getBar(); } and I want to filter criteria on A, two filters on different subclass properties, like: Criteria criteriaA = session.createCriteria(A.class); Criteria criteriaB = criteriaA.createCriteria("b").add(Restrictions.eq("foo", "Something")); Criteria criteriaC = criteriaA.createCriteria("c").add(Restrictions.eq("bar", 0)); What I want to do is combine criteriaB and criteriaC using an

Hibernate query a foreign key field with ID

不想你离开。 提交于 2019-12-03 04:32:43
问题 For example, I have two entities: Employee and Address. Of these enitities, Employee has a foreign key AddressID references the ID column on Address. In the Java domain objects, Hibernate nicely wraps the forgein key integer field with a Address object field. But now, how could I query the Employee with a certain AddressID? I have tried to create a table alias. That seems to work, but it is fairly awkward. I had also tried to do something like this: criteria.add(restriction.eq(

CriteriaBuilder.and & CriteriaBuilder.or, how-to?

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to change the following HQL to use JPA Criteria: select distinct d from Department d left join fetch d.children c where d.parent is null and ( d.name like :term or c.name like :term ) order by d.name Department has a Set<Department> of children. Criteria: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<Department> c = cb.createQuery(Department.class); Root<Department> root = c.from(Department.class); root.fetch("children", JoinType.LEFT); Path<Department> children = root.join("children", JoinType.LEFT);

Linq to NHibernate vs. ICriteria

匿名 (未验证) 提交于 2019-12-03 02:50:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I use LINQ a lot in general, especially LINQ-to-Objects, hence I'm rather fluent in LINQ. I was considering to use LINQ-to-NHibernate as the query language for my NHibernate project. When I wrote some tests, I noticed that LINQ-to-NHibernate doesn't make the same query as ICriteria. Since I'd prefer to use LINQ, I'd like to ask if anyone knows of similar differences, or should I just not bother about performance in general (The high-performance operations would need some tweaking with NHibernate anyway as far as I get it). See the following

Criteria Builder Create new Object In Select Statement

匿名 (未验证) 提交于 2019-12-03 02:49:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I was wondering if it's possible to create such query like : em.createQuery( "SELECT NEW EmpMenu(p.name, p.department.name) " + "FROM Project p ").getResultList(); also is it possible to do it via Specification: public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) { return ???; } Thanks in advance! 回答1: Yes, Criteria API does have have construct similar to JPQL constructor expressions. Resuls class is set via construct method in CriteriaBuilder. Your JPQL query expressed as an criteria query is:

Hibernate criteria query on different properties of different objects

匿名 (未验证) 提交于 2019-12-03 02:45:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Suppose I have classes like: class A { B getB(); C getC(); } class B { String getFoo(); } class C { int getBar(); } and I want to filter criteria on A, two filters on different subclass properties, like: Criteria criteriaA = session.createCriteria(A.class); Criteria criteriaB = criteriaA.createCriteria("b").add(Restrictions.eq("foo", "Something")); Criteria criteriaC = criteriaA.createCriteria("c").add(Restrictions.eq("bar", 0)); What I want to do is combine criteriaB and criteriaC using an "or" clause, something like: //this does not work

How to use Hibernate eqOrIsNull()

匿名 (未验证) 提交于 2019-12-03 02:30:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have two rows in MySQL like this +---------+---------+ | foo | bar | +---------+---------+ | | NULL | | | | +---------+---------+ Where empty are empty strings "" . Now I want to get both of them. I use Criteria and Restrictions.eqOrIsNull() on both columns, but it always returns only one row. The code is like this criteria.add(Restrictions.eqOrIsNull("foo", "")); .add(Restrictions.eqOrIsNull("bar", "")); And when I add criteria only on foo , it returns two rows. But for bar , it only returns the second, which is empty. The javadoc says,

conversion sql query to jpa

匿名 (未验证) 提交于 2019-12-03 02:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a query SELECT d.name, count(e.id) FROM department d LEFT OUTER JOIN employee e on e.department_id = d.id and e.salary > 5000 and how i can convert this to jpa right now i have: CriteriaQuery<Object[]> criteria = builder.createQuery(Object[].class); Root<Department> root = criteria.from(Department.class); Path<String> name = root.get("name"); Expression<Long> empCount = builder.count(root.get("employees").get("id")); criteria.multiselect(name,empCount); TypedQuery<Object[]> query = em.createQuery(criteria); I simplified both examples

how to perform union clause query with hibernate criteria api

匿名 (未验证) 提交于 2019-12-03 02:27:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: SELECT supplier_id FROM suppliers UNION ALL SELECT supplier_id FROM orders; i just creating two criteria above "UNION ALL" clause of query and below "UNION ALL" clause of query. but my question is how i perform UNION ALL clause in criteria? Thanks in Advance. 回答1: with criteria I think hibernate does not support UNION ALL but you can use two criteria queries to get the expected result: Criteria cr1 = session.createCriteria(Suppliers.class); cr1.setProjection(Projections.projectionList() .add( Projections.property("supplier_id"), "supplier_id