criteria

NHIbernate OR Criteria Query

☆樱花仙子☆ 提交于 2019-11-30 08:57:35
I have the following mapped classes Trade { ID, AccountFrom, AccountTo } Account {ID, Company} Company {ID} Now I cannot figure out a way select all trades where AccountFrom.Company.ID = X OR AccountTo.Company.ID = X I can get AND to work using the following: criteria.CreateCriteria("AccountFrom").CreateCriteria("Company").Add(Restrictions.Eq("ID", X); criteria.CreateCriteria("AccountTo").CreateCriteria("Company").Add(Restrictions.Eq("ID", X); But how can I transform this into an OR rather an an AND. I have used Disjunction previously, but I cannot seem to know how to add separate criteria,

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 use hibernate criteria to return only one element of an object instead the entire object?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 06:05:59
I'm trying to get only the list of id of object bob for example instead of the list of bob. It's ok with a HQL request, but I would know if it's possible using criteria ? An example : final StringBuilder hql = new StringBuilder(); hql.append( "select bob.id from " ) .append( bob.class.getName() ).append( " bob " ) .append( "where bob.id > 10"); final Query query = session.createQuery( hql.toString() ); return query.list(); I think you could do that with Projections, something like Criteria.forClass(bob.class.getName()) .add(Restrictions.gt("id", 10)) .setProjection(Projections.property("id"))

NHibernate 2.1: LEFT JOIN on SubQuery with Alias (ICriteria)

自古美人都是妖i 提交于 2019-11-30 05:20:05
问题 I am basically trying to create this query with NHibernate ICriteria interface: SomeTable 1:n AnotherTable SomeTable has columns: PrimaryKey, NonAggregateColumn AnotherTable has columns: PrimaryKey, ForeignKey, AnotherNonAggregate, YetAnotherNonAggregate SELECT table1.NonAggregateColumn, subquery.SubQueryAggregate1, subquery.SubQueryAggregate2 FROM SomeTable AS table1 LEFT JOIN ( SELECT table2.ForeignKey, COUNT(table2.AnotherNonAggregate) AS SubQueryAggregate1, AVG(table2

jpa criteria for many to many relationship

天涯浪子 提交于 2019-11-30 04:52:26
I have 2 POJO classes in Java, Answer and Collaborator, in a many-to-many relationship. class Answer { @ManyToMany(cascade = CascadeType.ALL) @JoinTable(name = "ANSWERS_COLLABORATORS", joinColumns = { @JoinColumn(name = "aid") }, inverseJoinColumns = { @JoinColumn(name = "cid") }) private Set<Collaborator> collaborators = new HashSet<Collaborator>(0); } Class Answer has a set of Collaborator , but a Collaborator doesn't keep a set of Answer . What I need to do from Hibernate CriteriaQuery is to find the collaborators for an answer given by id. I have already done this with Hibernate Criteria (

Grails/Hibernate: how to order by isnull(property) to get NULLs last?

跟風遠走 提交于 2019-11-30 04:45:09
问题 Normally when ordering ascending by a field, you get the NULL values first, and then the more interesting values. Often, you want NULL values last. In MySQL, you can do this with: SELECT * FROM people ORDER BY ISNULL(name), name; However, I'm using Grails with Hibernate criteria, and I have absolutely no idea how to do this there. Is this even supported in any way? Is there some way to order by a custom SQL expression? I'd hate to rewrite all my criteria to plain SQL just to get it to sort

Grails: Projection on many tables?

耗尽温柔 提交于 2019-11-30 03:55:30
问题 I have some problems with projection in Grails. Could you please help me review them and suggest solutions for me? I want to query data on many tables which has many-to-one relationship and projection on some properties on both of them. For example: class Person { int id String name String address static hasMany = [cars : Car] } class Car { int id String brand long price Person owner static belongsTo = [owner : Person] } So, how can I use just one query withCriteria (apply projection) to get

Doctrine 2.3 Criteria. Accessing a related Object

萝らか妹 提交于 2019-11-30 02:46:07
问题 I am trying to set up a Criteria according to the Doctrine Docs. Unfortunately they don't tell you how to access attributes of an related Object. Let me give you an example. I have an ArrayCollection of Products. Every Product has a Category. I want to filter the ArrayCollection for a Category Name. Now I am trying to set up a Criteria as follows: $criteria = Criteria::create() ->where(Criteria::expr()->eq("category.name", "SomeCategoryName")); Now I get the following Exception: An exception

Hibernate criteria query with subquery joining two columns

廉价感情. 提交于 2019-11-30 01:28:29
问题 I have a table, "Quote", mapped in hibernate that has a composite key of an integer id and a date, and several additional columns. I'd like to write a criteria query that uses a DetachedCriteria to get the row for each id with the greatest date. In sql, I might write a query like SELECT * FROM Quote q1 INNER JOIN (SELECT id, max(date) as maxdate FROM Quote GROUP BY id, date) q2 ON q1.id = q2.id AND q1.date = q2.maxdate In hibernate, I think can create a DetachedCriteria for the "group by"

Hibernate Criteria Order By

我的未来我决定 提交于 2019-11-30 01:08:03
问题 I have a table called Gift, which has a one-to-many relationship to a table called ClickThrough - which indicates how many times that particular Gift has been clicked. I need to query for all of the Gift objects, ordered by ClickThrough count. I do not need the ClickThrough count returned, as I don't need anything done with it, I just want to use it for purposes of ordering. I need the query to return a List of Gift objects directly, just ordered by ClickThrough count. How do I do this using