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中的实体中选择使用@ElementCollection定义的本机集合_Jpa_Spring Data_Spring Data Jpa - Fatal编程技术网

从JPA中的实体中选择使用@ElementCollection定义的本机集合

从JPA中的实体中选择使用@ElementCollection定义的本机集合,jpa,spring-data,spring-data-jpa,Jpa,Spring Data,Spring Data Jpa,假设我们有一个这种类型的实体: @Entity @Table(name="User") public class User { @Id long id; @ElementCollection List<String> phones; } @实体 @表(name=“User”) 公共类用户{ @身份证 长id; @元素集合 列出电话; } 我的目标是为所有用户提供完整的电话列表,比如:“从用户x中选择x.phones” 我尝试构建一个Spring数据JPA查询:

假设我们有一个这种类型的实体:

@Entity
@Table(name="User")
public class User {
  @Id
  long id;

  @ElementCollection
  List<String> phones;
}
@实体
@表(name=“User”)
公共类用户{
@身份证
长id;
@元素集合
列出电话;
}
我的目标是为所有用户提供完整的电话列表,比如:“从用户x中选择x.phones”

我尝试构建一个Spring数据JPA查询:

@Query("SELECT x.phones FROM User x")
List<String> findAllPhones();
@Query(“从用户x中选择x.phones”)
列出findAllPhones();
但我有一个例外:

org.hibernate.QueryException: not an entity at org.hibernate.hql.internal.ast.tree.FromElementType.renderIdentifierSelect(FromElementType.java:188) org.hibernate.QueryException:不是实体 在org.hibernate.hql.internal.ast.tree.FromElementType.renderIdentifierSelect(FromElementType.java:188)上 我不得不求助于本机sql查询来解决这个问题。 否则,我必须将电话号码包装在一个新实体中,但我确实希望避免这种情况


有没有办法用普通的JPA或(甚至更好的)纯Spring数据JPA方法来解决这类问题?

我想你的尝试是不可能的,因为我尝试使用了投影,但它们在预期的线条上显示了相同的错误这是我的例外,不是实体[从org.User中选择generatedAlias0.groups作为generatedAlias0]你最终解决了你的问题吗?不,我没有。我求助于使用本机查询。