criteria

SQL 'LIKE' operator in Hibernate Criteria API

ε祈祈猫儿з 提交于 2019-11-27 17:42:08
问题 I want to implement some universal filter with Hibernate Criteria . It should work like LIKE operator from SQL: SELECT * FROM table WHERE table.ANYCOLOUMNHERE LIKE '%'||anyvaluehere||'%' I have Map<String, String> where key is a column name, and value is its value. I tried something like this: for (Entry<String, String> filter : filters.entrySet()) { crit.add(Restrictions.ilike(filter.getKey(), filter.getValue(), MatchMode.ANYWHERE)); } But when field type is not String , it causes java.lang

How do I write a MAX query with a where clause in JPA 2.0?

 ̄綄美尐妖づ 提交于 2019-11-27 17:36:26
问题 I'm using JPA 2.0. Hibernate 4.1.0.Final, and Java 6. How do I write a JPA query from the following psuedo-SQL? select max(e.dateProcessed) from Event e where e.org = myOrg And my domain object looks like the following: @GenericGenerator(name = "uuid-strategy", strategy = "org.mainco.subco.core.util.subcoUUIDGenerator") @Entity @Table(name = "sb__event", uniqueConstraints = { @UniqueConstraint(columnNames={"EVENT_ID"}) } ) public class Event { @Id @Column(name = "ID") @GeneratedValue

Hibernate Criteria API - adding a criterion: string should be in collection

扶醉桌前 提交于 2019-11-27 16:44:15
问题 I have to following entity object @Entity public class Foobar { ... private List<String> uuids; ... } Now I'd like to make a criteria query which would fetch all Foobar pojos whose uuids list contains the string "abc123", I'm just not sure how to make the appropriate criterion. 回答1: I assume you are using a version of Hibernate that implements JPA 2.0. Here's a JPA 2.0 solution that should work with any compliant implementation. Please annotate uuids with JPA's @ElementCollection annotation.

How can one delete NHibernate objects using a criteria?

强颜欢笑 提交于 2019-11-27 16:06:15
问题 This must be a simple question. Given a criteria, how one deletes the entities satisfying the criteria? The rationale: HQL and NH criteria are NHibernate specific constructs and as such they are server side DAL implementation details. I do not want them to "leak" to the client side. So, our client side provides LINQ expressions for the server to process. Up until now the requests where select requests and LINQ to NHibernate dealed with them just fine. However, there is a need to implement

Get record with max id, using Hibernate Criteria

别来无恙 提交于 2019-11-27 15:34:00
问题 Using Hibernate's Criteria API, I want to select the record within a table with the maximum value for a given column. I tried to use Projections, creating an alias for max(colunName) , then using it in restrictions.eq(), but it keeps telling me " invalid number ". What's the correct way to do that with Hibernate? 回答1: You can use a DetachedCriteria to express a subquery, something like this: DetachedCriteria maxId = DetachedCriteria.forClass(Foo.class) .setProjection( Projections.max("id") );

TKmybatis的框架介绍及使用方法

和自甴很熟 提交于 2019-11-27 12:41:33
最近项目使用了SpringBoot+TKMytis框架,期间遇到一些问题,顺便记一下。 一、框架配置 配置的话非常简单,我用的是SpringBoot,直接引入: <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.0.3-beta1</version></dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>4.0.0</version></dependency> Mybatis的以及分页插件等就不写了。 创建一个BaseMapper public interface BaseMapper<T> extends tk.mybatis.mapper.common.BaseMapper<T>, MySqlMapper<T>, IdsMapper<T>, ConditionMapper<T>,ExampleMapper<T> { } 这5个Mapper待会我会详细讲解。 创建BaseService<T>继承自BaseMapper<T> public interface BaseService<T

how to user year() and month() functions in NH Criteria API?

人盡茶涼 提交于 2019-11-27 11:48:13
问题 I need to use year() and month() functions in Criteria API to be able to express a business filter constrain. Expressions like cri.Add(Expression.Ge("year(Duration.DateFrom)", Year.Value)); cri.Add(Expression.Le("year(Duration.DateTo)", Year.Value)); obviously do not work - is there any solution how to achieve this? I know it's entirely possible in HQL, but I need to construct the query using criteria API because there're some additional processes processing the query adding sorting, paging

Group by month with criteria in Hibernate

天大地大妈咪最大 提交于 2019-11-27 10:51:03
问题 I'm trying to get a report using Criteria and ProjectionList, and I'm pretty new using this through hibernate. So I have this model: private Long _userId; private Category _category; private Long _companyId; private Double _amount; private Date _date; And I building the query using this: public List sumPaymentsByUserCategoryPeriod(Category category, Long userId,Integer period){ GregorianCalendar from = new GregorianCalendar(); from.add(Calendar.MONTH, -period); List<CategoryAmount> resultDTO=

JPA2: Case-insensitive like matching anywhere

人走茶凉 提交于 2019-11-27 10:42:25
问题 I have been using Hibernate Restrictions in JPA 1.0 ( Hibernate driver ). There is defined Restrictions.ilike("column","keyword", MatchMode.ANYWHERE) which tests if the keyword matching the column anywhere and it is case-insensitive. Now, I am using JPA 2.0 with EclipseLink as driver so I have to use "Restrictions" build-in JPA 2.0. I found CriteriaBuilder and method like , I have also found out how to make it matching anywhere ( although it is aweful and manual ), but still I haven't figured

Hibernate Criteria Restrictions AND / OR combination

时光怂恿深爱的人放手 提交于 2019-11-27 10:31:31
How would I achieve this using Hibernate Restrictions? (((A='X') and (B in('X',Y))) or ((A='Y') and (B='Z'))) 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)); For the new Criteria since version Hibernate 5.2: CriteriaBuilder criteriaBuilder = getSession().getCriteriaBuilder(); CriteriaQuery<SomeClass> criteriaQuery = criteriaBuilder.createQuery