[英]Hibernate Criteria API and field selectors?
In Hibernate, I like the Criteria API, but it doesn't seem to allow you to specify certain columns for a specific entity class. 在Hibernate中,我喜欢Criteria API,但似乎不允许您为特定的实体类指定某些列。 For instance, given the following table: 例如,给定下表:
log_entries
id : PRIMARY KEY AUTO INCREMENT INTEGER
timestamp : datetime
log_level : varchar(25)
originator : varchar(200)
message : text
How could I write the following SQL query using the Criteria API: 如何使用Criteria API编写以下SQL查询:
SELECT
timestamp,
log_level
FROM
log_entries
WHERE
timestamp > '2013-06-05 00:00:00'
AND
originator <> 'com.me.myapp.SomeObject'
you need to use ProjectionList to get specific columns records, follow the steps 您需要使用ProjectionList来获取特定的列记录,请按照以下步骤操作
Criteria criteria = session.createCriteria(LogEntries.class);
//select columns
ProjectionList columns = Projections.projectionList();
columns.add(Projections.property("timestamp"));
columns.add(Projections.property("log_level"));
criteria.setProjection(columns);
//conditions
Criterion timestamp = Restrictions.gt("timestamp", "2013-06-05 00:00:00");
Criterion originator = Restrictions.not(Restrictions.eq("originator", "com.me.myapp.SomeObject"));
LogicalExpression condition = Restrictions.and(timestamp, originator);
criteria.add(condition);
List results = criteria.list();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.