0

在 Hibernate 中,我喜欢 Criteria API,但它似乎不允许您为特定实体类指定某些列。例如,给定下表:

log_entries
    id : PRIMARY KEY AUTO INCREMENT INTEGER
    timestamp : datetime
    log_level : varchar(25)
    originator : varchar(200)
    message : text

如何使用 Criteria API 编写以下 SQL 查询:

SELECT
    timestamp,
    log_level
FROM
    log_entries
WHERE
    timestamp > '2013-06-05 00:00:00'
    AND
    originator <> 'com.me.myapp.SomeObject'
4

1 回答 1

0

您需要使用 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();
于 2013-07-25T06:33:21.813 回答