JPA 自定义实体类实现,修正已作废的SQLQuery.class、setResultTransformer方法

hibernate 5.2 之后,SQLQuery.class、setResultTransformer方法已作废,修正后的写法做个记录:

@Entity
public class TaskDTO implements Serializable {
        ……
}
@PersistenceContext
EntityManager entityManager;

public List<Map<?, ?>> getWarningTaskList(short taskClass) {
    final String sql = "SELECT t.barcode,t.received_code AS receivedCode," +
            " t.patient_name AS patientName,t.task_id AS taskId,t.test_name AS testName," +
            " t.report_date AS reportDate,tp.outing_date AS outingDate," +
            " tp.outing_return_date AS outingReturnDate,tp.outing_express_code AS outingExpressCode," +
            " tp.task_step AS taskStep,tp.task_step_name AS taskStepName FROM task t INNER JOIN task_process tp " +
            " ON t.task_id = tp.task_id AND t.is_up = 2 " +
            " WHERE tp.task_class = :taskClass " +
            " AND tp.warning_state = 20 " +
            " AND tp.task_state <> 90 " +
            " AND DATEDIFF(t.report_date,CURRENT_DATE ()) <= 8 " +
            " ORDER BY t.report_date ASC, t.received_code ASC";
    Session session = entityManager.unwrap(org.hibernate.Session.class);
    NativeQuery query = session.createNativeQuery(sql);
    query.addScalar("barcode", StandardBasicTypes.STRING);
    query.addScalar("receivedCode", StandardBasicTypes.STRING);
    query.addScalar("patientName", StandardBasicTypes.STRING);
    query.addScalar("taskId", StandardBasicTypes.LONG);
    query.addScalar("testName", StandardBasicTypes.STRING);
    query.addScalar("reportDate", StandardBasicTypes.TIMESTAMP);
    query.addScalar("outingDate", StandardBasicTypes.TIMESTAMP);
    query.addScalar("outingReturnDate", StandardBasicTypes.TIMESTAMP);
    query.addScalar("outingExpressCode", StandardBasicTypes.STRING);
    query.addScalar("taskStep", StandardBasicTypes.SHORT);
    query.addScalar("taskStepName", StandardBasicTypes.STRING);
    query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.aliasToBean(TaskDTO.class));
    query.setParameter("taskClass", taskClass);
    entityManager.close();
    return query.getResultList();
}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

JPA 自定义实体类实现,修正已作废的SQLQuery.class、setResultTransformer方法

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭