错误:无法为具有多个返回的查询创建TypedQuery [英] Error: Cannot create TypedQuery for query with more than one return

查看:1670
本文介绍了错误:无法为具有多个返回的查询创建TypedQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用java和jpa做函数searchBook。我有2个班,分别是媒体和书。本书扩展了媒体。我将数据保存在不同的表中。我尝试从以下查询中选择数据:

I try to do the function searchBook with java and jpa. I have 2 classes which are Media and Book. Book extends Media. And I keep the data in the different table. I try to select the data from the query below:

TypedQuery<Media> query = em.createQuery(
                "SELECT m.title, b.isbn, b.authors"
                        + " FROM Book b, Media m" + " WHERE b.isbn = :isbn"
                        + " OR lower(m.title) LIKE :title"
                        + " OR b.authors LIKE :authors", Media.class);
        query.setParameter("isbn", book.getisbn());
        query.setParameter("title", "%" + book.getTitle().toLowerCase()
                + "%");
        query.setParameter("authors", "%" + book.getAuthors() + "%");
        bookList = query.getResultList();

但是我收到错误:


java.lang.IllegalArgumentException:无法为具有多个返回的查询
创建TypedQuery

java.lang.IllegalArgumentException: Cannot create TypedQuery for query with more than one return

这是我第一次使用JPA。我找不到错误。

This is the first time I use JPA. I can't find the the mistake.

推荐答案

没有详细了解 Media Book ,我至少会解释你为什么会遇到这个例外。

Without goind into details about how Media and Book should be modeled, I will at least explain why you get this exception.

你正在做:

em.createQuery(someJPQL, Media.class);

这意味着:使用 someJPQL 创建查询,此查询将返回 Media 实体的实例。

This means: create a query using someJPQL, and this query will return instances of the Media entity.

但是您的 JPQL 是:

SELECT m.title, b.isbn, b.authors ...

因此查询不会返回Media类型的实体。它返回来自两个不同实体的三个字段。您的JPA引擎无法从这3列中神奇地创建Media实例。如果查询如下所示,查询将返回Media的实例:

So the query does not return entities of type Media. It returns three fields, from two different entities. There is no way your JPA engine could magically create instances of Media from these 3 columns. A query would return instances of Media if it looked like this:

select m from Media m ...

这篇关于错误:无法为具有多个返回的查询创建TypedQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆