criteria

How to use ICriteria with Enum properties in NHibernate

安稳与你 提交于 2019-12-10 10:36:29
问题 Hi I want to write a FindByExample(object o) method. So I tried this: public IList<T> FindByExample(T o) { return Session.CreateCriteria(typeof(T)).Add(Example.Create(o)).List<T>(); } (It's in a generic class) It should work fine, but if T has a property of an enum type, it throws this exception: "Type mismatch in NHibernate.Criterion.SimpleExpression: EnumProperty expected type System.Int32, actual type EnumType" The mapping is this: <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" ...>

What is better way to work with time, using Hibernate Criteria and Oracle?

二次信任 提交于 2019-12-10 10:27:08
问题 I want to select entity by Time. I have Oracle DBMS with field type DATE which contain date and time. This is my code. How to select data by Time criteria? Calendar timeCal = new GregorianCalendar(0,0,0,0,0,0); Date timeMin = timeCal.getTime(); timeCal.add(Calendar.HOUR_OF_DAY, 1); Date timeMax = timeCal.getTime(); if (minDate != null && maxDate != null) criteria.add(Restrictions.between("eventDate", minDate, maxDate)); if (onDate != null) { Calendar calendar = Calendar.getInstance();

nHibernate Criteria for selecting a parent if a child in a collection has a specific value

こ雲淡風輕ζ 提交于 2019-12-10 05:18:08
问题 If I have the following class structure what is the NHibernate criteria to select a parent if one of it's children has a specific name? public class Child { public int Id { get; set; } public int Name { get; set; } } public class Parent { public int Id { get; set; } public IList<Child> Children { get; set; } } 回答1: I'd just create an alias to the collection and add restrictions. var parentsWithKidName = session.CreateCriteria<Parent>() .CreateAlias("Children", "c", JoinType.InnerJoin) .Add

Getting “with clause can only reference columns in the driving table” when trying to write a JPA 2.1 left outer join

懵懂的女人 提交于 2019-12-10 03:12:34
问题 I’m using JPA 2.1 and Hibernate 4.3.6.Final. I’m trying to use CriteriaBuilder to write a left outer join with conditions, so I have final CriteriaBuilder cb = m_entityManager.getCriteriaBuilder(); CriteriaQuery<Message> query = cb.createQuery(Message.class); Root<Message> messageRoot = query.from(Message.class); final Join<Message, Group> groupJoin = messageRoot.join(Message_.group); final Join<Message, MessageReadDate> msgReadDateJoin = messageRoot.join(Message_.messageReads, JoinType.LEFT)

mybatis Example Criteria like 模糊查询

◇◆丶佛笑我妖孽 提交于 2019-12-10 01:00:56
用Mybatis代码生成工具会产生很多个XXXExample类,这些类的作用是什么? 查阅了很多资料,在这里总结归纳一下 简介 XXXExample类用于构造复杂的筛选条件 它包含一个名为Criteria的内部静态类,它包含将在where子句中一起结合的条件列表。 Criteria类的集合允许您生成几乎无限类型的where子句。 可以使用createCriteria方法或or方法创建Criteria对象。 当使用createCriteria方法创建第一个Criteria对象时,它会自动添加到Criteria对象列表中 - 如果不需要其他子句,则可以轻松编写简单的Where子句。使用or方法时,Criteria类将添加到所有实例的列表中。 官方建议仅使用or方法创建Criteria类。这种方法可以使代码更具可读性。 Criteria类 Criteria内部类包括每个字段的andXXX方法,以及每个标准SQL谓词,包括: 字段方法 含义 IS NULL 表示相关列必须为NULL IS NOT NULL 表示相关列不能为NULL =(等于) 表示相关列必须等于方法调用中传入的值 <>(不等于) 表示相关列不能等于方法调用中传入的值 >(大于) 表示相关列必须大于方法调用中传入的值 > =(大于或等于) 表示相关列必须大于或等于方法调用中传入的值 <(小于)

Mybatis-技术专区-Mapper接口以及Example的实例函数及详解

孤街醉人 提交于 2019-12-09 22:50:31
一.mapper接口中的方法解析 mapper接口中的函数及方法 int countByExample(UserExample example) thorws SQLException 按条件计数 int deleteByPrimaryKey(Integer id) thorws SQLException 按主键删除 int deleteByExample(UserExample example) thorws SQLException 按条件查询 String/Integer insert(User record) thorws SQLException 插入数据(返回值为ID) User selectByPrimaryKey(Integer id) thorws SQLException 按主键查询 List selectByExample(UserExample example) thorws SQLException 按条件查询 List selectByExampleWithBLOGs(UserExample example) thorws SQLException 按条件查询(包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生。 int updateByPrimaryKey(User record) thorws SQLException 按主键更新

Hibernate criteria for many-to-many entity property

为君一笑 提交于 2019-12-09 18:58:50
问题 @Entity class A { @ManyToMany private List<B> list; ... } @Entity class B { ... } I'd like to get list from A class using criteria (not sql query). Is it posible to do this? Projection in this case does not work. 回答1: Unfortunately, The Criteria only allows selecting the root entity, and not any joined entity. It would thus be easier if your ManyToMany was bidirectional. You could the use the criteria equivalent of select b from B b inner join b.as a where a = :theA If that's not an option, I

Write subquery in Criteria of nHibernate

此生再无相见时 提交于 2019-12-09 17:09:24
问题 I've read about subquery in Criteria, but I am still unable to grasp it properly. Here I am taking one example and if somebody can help me write that using subquery it will be great. Lets say we have table Employee{EmployeeId.(int),Name(string),Post(string),No_Of_years_working(int)} Now I want all the employees who are Managers and working for less than 10 years. I know that we can get the result without using subqueries but I want to use subquery just to understand how it works in criteria.

Cannot be cast to java.io.Serializable

六眼飞鱼酱① 提交于 2019-12-09 08:38:28
问题 I am currently using criteria to retrieve the details of a user, but when trying to query the details object with the right user, I get a ClassCastException. My Criteria Code; Criteria criteria = sess.createCriteria(UserDetails.class) criteria.add(Restrictions.eq("user.id", user.id)); I also tried using; Criteria criteria = sess.createCriteria(UserDetails.class) Criteria subCriteria = criteria.createCriteria("user"); subCriteria.add(Restrictions.eq("id", user.id)); Both give me the

Grails/GORM “in” criteria

怎甘沉沦 提交于 2019-12-09 07:56:58
问题 Is it possible to do an "in" criteria using the GORM criteria. I'm looking for the equivalent of the following SQL select * from Person where age in (20,21,22); If it was possible I guess the syntax would be something like: def results = Person.withCriteria { in "age", [20, 21, 22] } 回答1: The Grails createCriteria documentation includes an example of using the in clause: 'in'("holderAge",[18..65]) or not{'in'("holderAge",[18..65])} The documentation includes this note: Note: 'in' is a groovy