如何使用@ConstructorResult注释

时间:2014-03-26 14:06:42

标签: java jpa nativequery

我有一些本机查询,并希望将查询执行的结果映射到非实体POJO类的列表中:

@SqlResultSetMapping(
    name = "SomeMapping",
    classes = {
        @ConstructorResult(targetClass = SomeClass.class,
            columns = {
                @ColumnResult(name = "id", type = Integer.class),
                @ColumnResult(name = "NAME", type = String.class),
                @ColumnResult(name = "DATE_BEGIN", type = java.util.Date.class)
            }
        )
    }
)

public class SomeClass{
    private Integer id;
    private String name;
    private java.util.Date begDate;
    private java.util.Date endDate;

    public SomeClass(Integer id, String name, Date begDate){
        this.id = id;
        this.name = name;
        this.begDate = begDate;
    }

    //Getters & Setters ...
}

一般来说我想检索查询执行的结果:

String query = "SELECT " +
"A.id AS 'id', " +
"A.name AS 'NAME', " +
"A.begDate AS 'DATE_BEGIN' " + 
"FROM " +
"SomeTable A " +
"WHERE A.endDate < '2014-01-01'";

List<SomeClass> result = em.createNativeQuery(query, "SomeMapping").getResultList();

但我总是得到对象列表。如何检索我的POJO-s列表?

1 个答案:

答案 0 :(得分:1)

尝试在实体上移动@SqlResultSetMapping实体。