criteria

JPA: left join without @OneToMany annotations

不想你离开。 提交于 2019-12-08 04:33:10
问题 I have a OneToMany relationship in my DB but I don't want that Hibernate manages it directly. This relationships are translations, but a DTO represents itself a translated registry: @Entity @Table(name = "my_table") public class MyTable { @Id @Column(name = "id", nullable = false, unique = true) private Integer id; @Transient private String lang; @Transient private String text; // getters and setters ... } @Entity @Table(name = "my_table_translation") public class MyTableTranslation { @Id

Hibernate: Query By Example involving one-to-many relationship

心不动则不痛 提交于 2019-12-08 04:32:44
问题 I've recently started playing with the query by example component of the Criteria API and have run into a strange issue - an org.hibernate.QueryException is thrown when trying to perform a search. My scenarios is as follows: I have a class A, which as one of its properties has a set of instances of class B (Set< B> listOfBs). This is mapped as a one-to-many relationship in A. I was hoping to set a criteria query on an example instance of B, for example specifying all B's with a property value

How to a write a Criteria query with multiple joins involved

走远了吗. 提交于 2019-12-08 03:08:10
问题 I'm trying to code the following HQL query using the Criteria API: var userList = _session .CreateQuery("select u from User u where u.Role.ID=3 and u.Customer.ID=:cID") .SetInt32("cID", 1) .List<User>(); (3 NHibernate objects : User(ID, Name, Role, Customer), Role(ID, Name) and Customer(ID, Name). I tried the following but it doesn't work because NHibernate tries to find a Customer associated with a Role: var userList = _session .CreateCriteria(typeof(User)) .CreateCriteria("Role") .Add

NHLambdaExtensions: Create a Criterion object to add to ICriteria later

落花浮王杯 提交于 2019-12-08 02:15:18
问题 My application creates a dynamically generated query at runtime based on user input by creating Criterion objects e.g: ICriterion criterion = Restrictions.Eq("Name", "John"); ...... detachedCriteriaSomewhereElse.Add(criterion); How do I do this in NHLambdaExtensions? what I really need to do is ICriterion criterion = Restrictions.Eq<Person>(p=> p.Name == "John"); but this isn't valid. Is there any way to do this? 回答1: With the NHLambdaExtensions you have the SQLExpression class that lets you

What is the best approach to reuse multiple repository criterias?

六月ゝ 毕业季﹏ 提交于 2019-12-07 19:03:45
问题 I have an repository layer that have many methods combination, to match search criterias.. What is the best approach to reuse this criterias? I think that methods name like findByNameAndIdAndBirthdayAndAccounaNumber is not a good idea ! Thanks ! public Order findByIdAndName(String orderId) { List<OrderEntity> list = entityManager.createNamedQuery(OrderEntity.QUERY_FIND_BY_ORDERID_AND_NAME, OrderEntity.class) .setParameter("orderId", orderId) .setParameter("name", name).getResultList(); if

Using “Is Null” as a criteria based on a form

末鹿安然 提交于 2019-12-07 18:59:48
问题 I'm trying to get my query to only return null records if a radio box on a form is ticked. If I manually include the criteria Is Null in the query it works just fine, and elsewhere things like IIf([Forms]![Reports]![Status - Active]=True,"Active") work just fine. However, IIf([Forms]![Reports]![Status - Null]=True,"Is Null") just ignores it (I assume because it's actually looking for the text "Is Null" rather than null records) and IIf([Forms]![Reports]![Status - Null]=True,Is Null) fails

Removing Order from NHibernate Criteria Query

有些话、适合烂在心里 提交于 2019-12-07 17:43:34
问题 I have a criteria query that I am using to show pages of results. I also need to obtain the total count of all items. Rather than have two queries, one for paging the results and one for the count (since they are identical apart from the .AddOrder() public ICriteria StandardQuery { get { return NHibernateSesssionManager.GetSession.CreateCriteria<Person>.AddOrder("OrderProperty", Order.Desc); } public ICriteria CountQuery { get{ return StandardQuery.SetProjection(Projections.Count("ID")); }

Math operators in Criteria queries

一曲冷凌霜 提交于 2019-12-07 17:31:23
问题 Given the mapped hibernate class: @Entity public class MyTestClass { /* id and stuff */ private Integer aValue; private Integer bValue; } you can do the following with HQL: Query query = getCurrentSession().createQuery("select aValue * bValue from MyTestClass"); List<Double> resultList = query.list; and get the calculated result out. Is it possible to do something similar to this with the Criteria API? I still haven't found a way to use math operations with the Criteria API. We have aggregate

NHibernate: Convert an ICriteria to a DetachedCriteria

笑着哭i 提交于 2019-12-07 17:04:09
问题 Anyone know how to convert an ICriteria into a DetachedCriteria. I need to use an existing ICriteria as part of a subquery using: .Add(Subqueries.PropertyIn("Name", myDetachedCriteriaSubquery)) Is there any way to convert an ICriteria to a DetachedCriteria. I will accept 'no' with a credible reference. 回答1: Following on from mattk's answer, you can inherit DetachedCriteria to access its constructors: public class ConvertedDetachedCriteria : DetachedCriteria { public ConvertedDetachedCriteria

Issue with selecting max id rows using criteria query / hibernate query?

断了今生、忘了曾经 提交于 2019-12-07 16:47:19
问题 I am unable to select the rows where TestId is max for respective student, I wrote the code as follows which does not get the required output. my code is as follows, Criteria c = sessionFactory.getCurrentSession().createCriteria(student.class).setProjection(Projections.projectionList().add(Projections.property("answer"),"answer")); c.add(Restrictions.eq("surveyId",send_Survey)); //c.add(Restrictions.eq("testId", "1" )); //c.setProjection(Projection.max("testId")); c.addOrder(Order.desc(