What is the resultlist of a named query when not returning all columns

700 views Asked by At

I have a named query on an entity that does not get the full table (*), but only 2 columns of it. How do I implement this named query and what is the return type of the 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;

and

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();
    }

This returns an error:

Caused by: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.namespace.text.TextEntity

How would I do this when I don't get the whole table from the named query statement, but only two String columns?

0

There are 0 answers