Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jpa 当不返回所有列时,命名查询的结果列表是什么_Jpa_Named Query - Fatal编程技术网

Jpa 当不返回所有列时,命名查询的结果列表是什么

Jpa 当不返回所有列时,命名查询的结果列表是什么,jpa,named-query,Jpa,Named Query,我在一个实体上有一个命名查询,该实体没有得到完整的表*,但只得到其中的2列。如何实现这个命名查询以及列表的返回类型是什么 @NamedQueries({ @NamedQuery(name="Text.testQuery", query = "SELECT DISTINCT t.col1, t.col2 FROM TextEntity t WHERE t.col1 = :type AND t.col2 LIKE :searchString") }) @Entity @Table(n

我在一个实体上有一个命名查询,该实体没有得到完整的表*,但只得到其中的2列。如何实现这个命名查询以及列表的返回类型是什么

@NamedQueries({
        @NamedQuery(name="Text.testQuery", query = "SELECT DISTINCT t.col1, t.col2 FROM TextEntity t WHERE t.col1 = :type AND t.col2 LIKE :searchString")
})
@Entity
@Table(name = "TEXT")
public class TextEntity implements Serializable

    @Id
    @Column(length = 36)
    private String id;
    @Column(length = 16)
    private String col1 = null;
    @Column(length = 3)
    private String col2 = null;
    @Column(length = 2)
    private String col3 = null;
    @Column(length = 255)
    private String col4 = null;


如果我没有从命名查询语句中获取整个表,而只有两个字符串列,我将如何执行此操作?

这是异常消息指示的列表。感谢您的回答。对象数组提示非常有用,我不敢相信我只是跳过了它,没有考虑它。现在,我可以通过这些对象数组的列表访问这两列。不过,我认为可能有一个更方便/安全的解决方案。如果需要,您还可以使用映射到结果列表的类或元组查询来指定。这个例子是关于JPA标准API的,但同样的东西也适用于JPQL。
public List<TextEntity> findErpIdByTypeAndSearchString(IEntity.Type type, String searchString, int maxResults) {
        Query query = em.createNamedQuery("Text.testQuery");
        query.setParameter("type", type);
        query.setParameter("searchString", "%" + searchString + "%");
        query.setMaxResults(maxResults);

        return query.getResultList();
    }
Caused by: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.namespace.text.TextEntity