一、环境
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()); } } }
输出结果
数据库记录
好了就写到这吧