criteria

Hibernate Criteria, Integer and “like”

杀马特。学长 韩版系。学妹 提交于 2019-12-01 16:41:24
I'm migrating some of my hql-statements to Criterias now I'm figuring one problem: The entity property is type Integer but I need a like with wildcards search, so in hql I do session.createQuery("from P1 where id like :id").setString("id", "%"+s+"%") No problem at all, Hibernate casts String to Integer. If I try this in Criteria, I only get a ClassCastException String cannot be cast to Integer Criteria crit = sessionFactory.getCurrentSession().createCriteria(P1.class); crit.add(Restrictions.like("id",s)).addOrder(Order.asc("id")).setMaxResults(maxResults); Why does Hibernate handle both

How to set more than 2 Expression in Expression.Or

时光怂恿深爱的人放手 提交于 2019-12-01 16:26:15
I want to create a query which has more than 3-4 Expression.Or ? But Expression.Or just let me to add two Expressions inside it. if (!string.IsNullOrEmpty(keyword)) query .Add(Expression.Or( Expression.Like("Name", keyword, MatchMode.Anywhere), Expression.Like("LastName", keyword, MatchMode.Anywhere))) .Add(Expression.Or( Expression.Like("Email1", keyword, MatchMode.Anywhere), Expression.Like("Email2", keyword, MatchMode.Anywhere))); The code above generates "Name like %this% or LastName like %this% AND Email1 like %this% and Email2 like %this. Thanks in advance. Use Disjunction instead of Or.

Hibernate Criteria, Integer and “like”

最后都变了- 提交于 2019-12-01 15:58:01
问题 I'm migrating some of my hql-statements to Criterias now I'm figuring one problem: The entity property is type Integer but I need a like with wildcards search, so in hql I do session.createQuery("from P1 where id like :id").setString("id", "%"+s+"%") No problem at all, Hibernate casts String to Integer. If I try this in Criteria, I only get a ClassCastException String cannot be cast to Integer Criteria crit = sessionFactory.getCurrentSession().createCriteria(P1.class); crit.add(Restrictions

Order by relation count in NHibernate

烂漫一生 提交于 2019-12-01 14:58:37
I have a datastructure like this: public class User { public Guid Id {get;set;} public string Name {get;set;} public IList<Books> Books {get;set} } I have been struggeling with making it possible to sort the users by the count of bookmarks (one-to-many relation). I have tried various approaches with both linq, criteria and queryover, but with no luck, and therefore hope one of you could help. I am using paging, since I have quite a few users, so the solution needs to do the query on the SQL and not in memory on the webserver. var loadedUser = session.Query<User>() .Select(u => new { u, u.Books

Using sum and arithmetic result as order key in hibernate criteria

荒凉一梦 提交于 2019-12-01 14:27:14
How can I express this query in hibernate criteria. SELECT anId, SUM(fieldA) AS A, SUM(fieldB) AS B, SUM(fieldA)+SUM(fieldB) AS 'total' FROM tableA GROUP BY anId ORDER BY 'total' DESC LIMIT 5 following criteria should do the trick Criteria criteria = hibernateSession .createCriteria(YourEntity.class); criteria.setProjection(Projections .projectionList() .add(Projections.property("anId").as("prop1")) .add(Projections.sum("fieldA").as("prop2")) .add(Projections.sum("fieldB").as("prop3")) .add(Projections.sqlProjection( "sum(fieldA) + sum(fieldB) as total", new String[] { "total" }, new Type[] {

Using sum and arithmetic result as order key in hibernate criteria

一世执手 提交于 2019-12-01 13:12:33
问题 How can I express this query in hibernate criteria. SELECT anId, SUM(fieldA) AS A, SUM(fieldB) AS B, SUM(fieldA)+SUM(fieldB) AS 'total' FROM tableA GROUP BY anId ORDER BY 'total' DESC LIMIT 5 回答1: following criteria should do the trick Criteria criteria = hibernateSession .createCriteria(YourEntity.class); criteria.setProjection(Projections .projectionList() .add(Projections.property("anId").as("prop1")) .add(Projections.sum("fieldA").as("prop2")) .add(Projections.sum("fieldB").as("prop3"))

springboot调用mongo

旧城冷巷雨未停 提交于 2019-12-01 12:41:56
目录 添加 删除 文档操作更新 简单聚合操作 count, distinct 普通查询 分组 group Aggregate mapReduce 分页查询 上传上传 文件下载 随便测试了一下,有问题请指出 pom.xml中添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.2</version> </dependency> application.yml中添加连接信息 spring: data: mongodb: uri: mongodb://xxx:27017/school 测试类 @SpringBootTest @RunWith(SpringRunner.class) public class MongoDBTest { @Autowired private MongoTemplate mongoTemplate; /*@Autowired private

hibernate query language or using criteria?

☆樱花仙子☆ 提交于 2019-12-01 11:13:49
Any one who tell me the query using criteria/hql/sql. Requirement is that user enter email or username the query return the password of the user from table user. If all you're doing is fetching one field, you probably just want to go hql (or possibly sql). If you do criteria, I believe you're pulling back the entire object, just to eventually use one field. Edit: That's a really broad question. Here is a tutorial The Criteria API is very appropriate for dynamic query generation and would have my preference here. You could do something like this: Criteria criteria = session.createCriteria(User

NHibernate: can't successfully set lazy loading

限于喜欢 提交于 2019-12-01 10:45:41
I have a table Parent and a table Child. Child contains a foreign-key to the Parent table, creating a one-to-many relationship. Here is a part of my mapping that I define with fluent NHibernate: public class ParentMap : ClassMap<Parent> { public ParentMap() { WithTable("Parents"); Id(x => x.Id, "ParentID") .WithUnsavedValue(0) .GeneratedBy.Identity(); Map(x => x.Description, "Description"); HasMany<Child>(x => x.Childs) .LazyLoad() .WithKeyColumn("ParentID") .IsInverse() .AsSet(); } } public class ChildMap : ClassMap<Child> { public ChildMap() { WithTable("Childs"); Id(x => x.Id, "ChildID")

NHibernate query for matching all tags

 ̄綄美尐妖づ 提交于 2019-12-01 10:35:00
问题 Here are my relevant classes: public class Item { public virtual int Id { get; protected set; } public virtual IList<Tag> Tags { get; set; } } public class Tags { public virtual int Id { get; protected set; } public virtual string Name { get; set; } public virtual IList<Item> Items { get; set; } } These are mapped with a many to many association. The intermediate table is named ItemsToTags. Here's the question: Given a list of strings, how do I create an NHibernate query that returns all Item