Selecting your own collections defined with @ElementCollection from Entities in JPA

Let's assume 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 a complete list of phones for all users, for example: "SELECT x.phones FROM User x"

I tried to build a JPA data query like this:

@Query("SELECT x.phones FROM User x")
List<String> findAllPhones();

      

But I am getting 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 my own SQL query to solve the problem. Otherwise I will have to wrap the phone number inside the new object, but I definitely want to avoid that.

Is there a way to solve this problem using only JPA, or (even better) only Spring JPA Data Methods?

+3
spring-data spring-data-jpa jpa


source to share


No one has answered this question yet

See similar questions:

7
JPA 2 - Using @ElementCollection in CriteriaQuery

or similar:

341
Create the perfect JPA object
167
How to update an object using spring-data-jpa?
39
Spring JPA data query with parameters
8
Spring JPA Data Projecting Selected Fields from DB
6
How to convert joda DateTime to original jpa request
2
XML entity mapping error in Spring Data Jpa
2
JPA Query with collection where operator
2
Spring JPA with Counting Collection
1
Spring data jpa native query with join
0
How can I get data for a column based entity from another entity using Spring JPA data?



All Articles
Loading...
X
Show
Funny
Dev
Pics