Let's suppose we have an Entity of this type:
@Entity
@Table(name="User")
public class User {
@Id
long id;
@ElementCollection
List<String> phones;
}
My goal is to have the complete list of phones, for all users, something like: "SELECT x.phones FROM User x"
I tried to build a Spring Data JPA query of this kind:
@Query("SELECT x.phones FROM User x")
List<String> findAllPhones();
But I get this exception:
org.hibernate.QueryException: not an entity at org.hibernate.hql.internal.ast.tree.FromElementType.renderIdentifierSelect(FromElementType.java:188)
I had to resort to a native sql query to solve the problem. Otherwise I have to wrap the phone number inside a new Entity, but I exactly want to avoid that.
Is there any way to solve this kind of problem using plain JPA or (even better) only Spring Data JPA methods?