criteria

Querying ManyToMany relationship with Hibernate Criteria

走远了吗. 提交于 2019-11-26 20:02:54
问题 I'm not sure how to describe this problem, so I think an example is the best way to ask my question: I have two tables with a manyToMany relationship: DriversLicence <-> LicenceClass LicenceClass is things like "Car", "Motorbike", and "Medium Rigid". Using Hibernate Criteria, how can I find all licences that have both "Car" and "Motorbike" LicenceClasses? UPDATE 12/11/2008 I have discovered that this can easily be achieved by using a custom ResultTransformer. However the problem is that a

Hibernate Criteria and multiple join

送分小仙女□ 提交于 2019-11-26 19:46:35
问题 is possible with Hibernate criteria do it? select A.something, B.something, C.something, D.something from A JOIN B on A.id = B.id_fk JOIN C ON B.id = C.id_fk JOIN D ON C.id = D.id_fk; 回答1: I have got exactly the same problem, and was able to resolve it like this: return criteria.createCriteria(A.class) .createCriteria("b", "join_between_a_b") .createCriteria("c", "join_between_b_c") .createCriteria("d", "join_between_c_d") .add(Restrictions.eq("some_field_of_D", someValue)); Note: "b" , "c"

Hibernate Criteria Restrictions AND / OR combination

只愿长相守 提交于 2019-11-26 17:57:18
问题 How would I achieve this using Hibernate Restrictions? (((A='X') and (B in('X',Y))) or ((A='Y') and (B='Z'))) 回答1: think works Criteria criteria = getSession().createCriteria(clazz); Criterion rest1= Restrictions.and(Restrictions.eq(A, "X"), Restrictions.in("B", Arrays.asList("X",Y))); Criterion rest2= Restrictions.and(Restrictions.eq(A, "Y"), Restrictions.eq(B, "Z")); criteria.add(Restrictions.or(rest1, rest2)); 回答2: For the new Criteria since version Hibernate 5.2: CriteriaBuilder

How to properly express JPQL “join fetch” with “where” clause as JPA 2 CriteriaQuery?

拜拜、爱过 提交于 2019-11-26 17:22:54
Consider the following JPQL query: SELECT foo FROM Foo foo INNER JOIN FETCH foo.bar bar WHERE bar.baz = :baz I'm trying to translate this into a Critieria query. This is as far as I have gotten: CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Foo> cq = cb.createQuery(Foo.class); Root<Foo> r = cq.from(Foo.class); Fetch<Foo, Bar> fetch = r.fetch(Foo_.bar, JoinType.INNER); Join<Foo, Bar> join = r.join(Foo_.bar, JoinType.INNER); cq.where(cb.equal(join.get(Bar_.baz), value); The obvious problem here is that I am doing the same join twice, because Fetch<Foo, Bar> doesn't seem to have a

UNION to JPA Query

社会主义新天地 提交于 2019-11-26 14:27:34
问题 Is it possible to query "UNION" in JPA and even "Criteria Builder"? I'm looking for examples, but so far i got no result. Does anyone have any examples of how to use it? Or would that be with native sql? 回答1: SQL supports UNION, but JPA 2.0 JPQL does not. Most unions can be done in terms of joins, but some can not, and some are more difficult to express using joins. EclipseLink supports UNION. 回答2: Depending on the case, one could use sub queries, something like: select e from Entity e where

Hibernate Criteria Join with 3 Tables

久未见 提交于 2019-11-26 14:23:48
I am looking for a hibernate criteria to get following: Dokument.class is mapped to Role roleId Role.class has a ContactPerson contactId Contact.class FirstName LastName I want to search for First or LastName on the Contact class and retrieve a list of Dokuments connected. I have tried something like this: session.createCriteria(Dokument.class) .setFetchMode("role",FetchMode.JOIN) .setFetchMode("contact",FetchMode.JOIN) .add(Restrictions.eq("LastName","Test")).list(); I get an error could not resolve property "LastName" for class "Dokument" Can someone explain why the join searches on Dokument

JPA and Hibernate - Criteria vs. JPQL or HQL

 ̄綄美尐妖づ 提交于 2019-11-26 12:34:05
What are the pros and cons of using Criteria or HQL ? The Criteria API is a nice object-oriented way to express queries in Hibernate, but sometimes Criteria Queries are more difficult to understand/build than HQL. When do you use Criteria and when HQL? What do you prefer in which use cases? Or is it just a matter of taste? I mostly prefer Criteria Queries for dynamic queries. For example it is much easier to add some ordering dynamically or leave some parts (e.g. restrictions) out depending on some parameter. On the other hand I'm using HQL for static and complex queries, because it's much

HIbernate常用的三种查询

只愿长相守 提交于 2019-11-26 12:21:54
Hibernate三种常用查询API:标准,HQL,SQL 使用工具: hibernate的核心jar包及配置文件和映射文件 log4j打印日志文件及jar及配置文件 3.eclipse开发,navicat数据库可视化工具,mysql 4.连接数据库的jar包 注:详细信息页面最下面(本篇只是初略起稿,不足之处,望帮忙指出,谢谢) 1.标准的查询:使用hibernate封装好的方法,完成查询所需: 1.1查询所有数据: public void SelectLike() { // 获取session Session session = HibernateUtil.openSession(); // 查询主表信息: Criteria criteria = session.createCriteria(ZJ.class); //返回查询结果并返回给集合 List list = criteria.list(); //打印信息 System.out.println(list); } 1.2精准条件查询: @Test public void SelectLike() { // 获取session Session session = HibernateUtil.openSession(); // 查询主表信息: Criteria criteria = session.createCriteria

NHibernate - CreateCriteria vs CreateAlias

*爱你&永不变心* 提交于 2019-11-26 12:09:09
问题 Assuming the following scenario: class Project{ public Job Job; } class Job{ public Name; } Assuming I want to use the Criteria API to search for all projects whose Job has the name \"sumthing\". I could use the CreateAlias to create an alias for Job and use it to access Name, or I could create a new Criteria for the property Job and search by Name. Performance wise, is there any difference? 回答1: given these requirements there would be no difference, the generated SQL is the same: for

How to get SQL from Hibernate Criteria API (*not* for logging)

自作多情 提交于 2019-11-26 11:39:34
is there an easy way to get the (to-be-generated) sql from a Hibernate Criteria? Ideally I would have something like: Criteria criteria = session.createCriteria(Operator.class); ... build up the criteria ... ... and then do something like ... String sql = criteria.toSql() (But this of course does not exist) The idea would then be to use the sql as part of a huge 'MINUS' query (I need to find the differences between 2 identical schemas - identical in structure, not in data - and the MINUS is not supported by Hibernate) (BTW I know I can check the SQL from the log files) I've done something like