JPA NamedQuery 获取不同列
我想从我的用户表中获取不同城市的列表。我认为下面的代码可以工作,但给出了一个错误:
User.java
@Entity
@Table(name="user_info")
...
@NamedQuery(name = "User.all.cities", query = "SELECT distinct u.city FROM User u"),
...
@embedded
private City city;
UserBusinessLogic.java:
...
TypedQuery<City> typedQuery = entityManager.createNamedQuery("User.all.cities",User.class);
List<City> names = typedQuery.getResultList();
...
它给出:类型不匹配无法将列表转换为列表。我尝试了两次获取第一个用户,然后在 getResult 上尝试了一个城市,但下面一行出现了相同的错误。
我看到了一些例子,但并没有真正告诉我如何使用正确的代码(仅是 SQL 语法)来获取它。
谢谢你的帮助
杰西
I want to get an list of distinct City's from my User table. I thought the code below would work but gives an error:
User.java
@Entity
@Table(name="user_info")
...
@NamedQuery(name = "User.all.cities", query = "SELECT distinct u.city FROM User u"),
...
@embedded
private City city;
UserBusinessLogic.java:
...
TypedQuery<City> typedQuery = entityManager.createNamedQuery("User.all.cities",User.class);
List<City> names = typedQuery.getResultList();
...
It gives: type mismatch can not convert List to List. I tried two get first user then on getResult a City but same error one line below.
I see some examples but not really tell how to get it with correct code just the SQL syntax.
Thanks for your help
Jess
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一件看起来不太好的事情是,您请求 City 对象,但声明获取 User 对象。
你有:
它应该在哪里 是:
The first thing which doesn't look good is that you ask for City objects but declare to get User's.
You have:
Where it should be:
有效的代码是:
The code that works is: