在 jpql select 中指定列会导致转换错误

发布于 2024-10-03 13:03:24 字数 347 浏览 0 评论 0 原文

当我在 jpql/jpa 2.0 查询中指定列时,即 select p.id, p.lastName, p.firstName from Profile p where p.group = :group 我收到以下错误: [Ljava.lang.Object;无法转换为 com.profs.ws.Profile... 有人知道如何解决此转换问题吗?

Profile 实体类本身具有以下类型的属性:String、int 和 Collection。我在查询中选择的属性是 String 类型或 int 类型。

When I specify columns in my jpql/jpa 2.0 query, i.e. select p.id, p.lastName, p.firstName from Profile p where p.group = :group I get the following error: [Ljava.lang.Object; cannot be cast to com.profs.ws.Profile... Anyone know how to resolve this casting issue?

The Profile entity class itself has properties of the following types: String, int, and Collection. The properties I'm selecting in the query are either of the type String or int.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧梦荧光笔 2024-10-10 13:03:24

当您指定对象的属性时,JPA 返回Object[] 列表。您可以将返回值转换为 List 而不是 List 以避免出现 ClassCastException。如果您使用 Hibernate 作为 JPA 提供程序,则可以将 select 子句映射到新对象。有关详细信息,请参阅 select 子句 文档。

When you specify properties of an object, JPA returns the list of Object[]. You can cast the return value to List<Object[] instead of List<Profile> to avoid the ClassCastException. If you are using Hibernate as JPA provider, you can map the select clause to a new object. See select clause documentation for details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文