我对没有获得完整表(*)的实体有一个命名查询,但只有2列。如何实现此命名查询以及List的返回类型是什么?
@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;
和
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
当我从命名查询语句中获取整个表但只有两个String列时,我该怎么做呢?