hibernate5 Cannot create TypedQuery for query with more than one return using requested result type

简介: hibernate5 Cannot create TypedQuery for query with more than one return using requested result type

一、环境

1、hibernate 5.3.1.Final

Maven POM格式

<!-- hibernate -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.3.1.Final</version>
    </dependency>

2、IDE IntelliJ IDEA 2020

版本很关键,不写版本号发技术文章就是耍流氓

二、BUG

Exception in thread "main" java.lang.IllegalArgumentException: Cannot create TypedQuery for query with more than one return using requested result type [com.yadinghao.entity.po.User]

说白了点就是类型不匹配

我的代码,实体类:

package com.yadinghao.entity.po;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity(name = "user")
@Data
public class User {
    public  String age;
    @Id
    public String name;
}

核心代码

package com.yadinghao.demo;
import com.yadinghao.entity.po.User;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import org.hibernate.service.ServiceRegistry;
import javax.jws.soap.SOAPBinding;
import java.sql.SQLException;
import java.util.List;
public class hibernatedemo {
    //POJO 创建数据库表
    public static void main(String[] args) {
        Configuration config = new Configuration();
        config.configure("hibernate.cfg.xml");
        // 创建工厂
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
        SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
        // creating session object
        Session session = sessionFactory.openSession();
        String sql = "select name,age from user";
        Query<User> query = session.createQuery(sql,User.class);
        List<User> list = query.list();
        System.out.println(list.size());
        for(User user : list){
            System.out.println(user.getName());
        }
    }
}

错误是这句

Query<User> query = session.createQuery(sql,User.class);

反正我是没搞懂为啥错误。把泛型去了就没事

Query query = session.createQuery(sql);

但是在转List或实体的时候会很麻烦。故此一通百度发现各种解决也都 不是我的最可恨的是有个人连Hibernate都不写,更别说版本号了.....

我的解决办法是把创建查询的createQuery改成createNativeQuery。

代码如下:

Query<User> query = session.createNativeQuery(sql,User.class);

完整代码

package com.yadinghao.demo;
import com.yadinghao.entity.po.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import org.hibernate.service.ServiceRegistry;
import java.util.List;
public class hibernatedemo {
    //POJO 创建数据库表
    public static void main(String[] args) {
        Configuration config = new Configuration();
        config.configure("hibernate.cfg.xml");
        // 创建工厂
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
        SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
        // creating session object
        Session session = sessionFactory.openSession();
        String sql = "select name,age from user";
        Query<User> query = session.createNativeQuery(sql,User.class);
        List<User> list = query.list();
        System.out.println(list.size());
        for(User user : list){
            System.out.println(user.getName());
        }
    }
}

输出结果

数据库记录

好了就写到这吧

目录
相关文章
|
Java 数据库连接
hibernate exception (cannot simultaneously fetch multiple bags)
      org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags at org.
1584 0
|
Java 数据库连接 网络安全
SSH整合时执行hibernate查询报错:java.lang.ClassCastException: com.ch.hibernate.Department_$$_javassist_0 cannot be cast to javassist.util.proxy
  今天在整合ssh三个框架时,有一个功能,是查询所有员工信息,且员工表和部门表是多对一的映射关系,代码能正常运行到查询得到一个List集合,但在页面展示的时候,就报异常了, java.lang.
1226 0
|
Java 数据库连接
解决 hibernate cannot define positional parameter after any named parameters have been defined
解决 hibernate  cannot define positional parameter after any named parameters have been defined 把模糊查询的条件放在最后开始做,坚持做,重复做
859 0
|
Java 数据库连接
Hibernate中HQL语句查询指定字段值报错:ClassCastException: [java.lang.Object; cannot be cast to bean.PersonStatic
在有一次通过如下HQL语句进行查找相应字段时: String hql = "select sum(search),sum(geometry),sum(greedy), sum(graph),sum(number),sum(simulate),sum(combine),sum(structure) from PersonStatis ps where ps.
1344 0
|
Java 数据库连接
使用Hibernate 过程中 报—— cannot simultaneously fetch multiple bags
解决方法有:   1.将fetchType改为懒加载   2.加上注解@Fetch(FetchMode.SUBSELECT)     @Fetch(FetchMode.SUBSELECT) public List getModels() { return this.
974 0