关于Hibernate中JUnit版本导致空指针异常问题

        遇到的问题描述:Hibernate中JUnit版本导致空指针异常问题

首先我说明一下我遇到的问题:初次使用Hibernate时,没有仔细听项目负责人介绍Hibernate,而使用了Hibernate中的JUnit5,以至于后面一直出现空指针异常,排
除也排除不了,代码找不到错误。
下面是我的数据库、工程和代码:
数据库:


关于Hibernate中JUnit版本导致空指针异常问题_第1张图片
图片.png

关于Hibernate中JUnit版本导致空指针异常问题_第2张图片
图片.png

工程:


关于Hibernate中JUnit版本导致空指针异常问题_第3张图片
1.png

代码:UserDao.java类
package com.qianfeng.dao;

import org.hibernate.Session;
import com.qianfeng.model.User;
import com.qianfeng.tools.DBUtil;

public class UserDao {
public User selectById(Integer id) {
Session session=DBUtil.findsession();
User user = (User) session.get(User.class,id);
return user;
}
}

User.java类:
package com.qianfeng.model;

import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String username;
private String password;
private Integer gender;
private Integer age;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public User() {
super();
}
}

User.hbm.xml文件:

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">













HibernateTest.java类:
package com.qianfeng.test;

//import org.junit.Before;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import com.qianfeng.dao.UserDao;
import com.qianfeng.model.User;

public class HibernateTest {
//UserDao dao;
/@Before
public static void init() {
dao=new UserDao();
}
/
static UserDao dao;
@BeforeAll
public static void init() {
dao=new UserDao();
}
@Test
public void test() {
System.out.println(dao);
User user=dao.selectById(1);
System.out.println(user); System.out.println(user.getId()+","+user.getUsername()+","+user.getPassword());
}
}

DBUtil.java类:
package com.qianfeng.tools;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class DBUtil {
static SessionFactory sf;
static {
Configuration cfg=new Configuration().configure("hibernate.cfg.xml");
sf = cfg.buildSessionFactory();
}
public static Session findsession() {
return sf.openSession();
}
public static void main(String[] args) {
//初始化Hibernaete.cfg.xml文件
Configuration cfg=new Configuration().configure("hibernate.cfg.xml");
//初始化SessionFactory工厂
SessionFactory sf = cfg.buildSessionFactory();
System.out.println(sf.openSession());
}
}

hibernate.cfg.xml文件:

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">



com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/javaee
root
123
org.hibernate.dialect.MySQLDialect

true

true



其中测试类中我使用的是JUnit5,因为负责人是JUnit4,所有我仔细审核了代码,发现代码是没有异样的,这里注意我使用的是被注释的HibernateTest类的@Before,故测试结果一直报User user=dao.selectById(1);这句空指针异常,我很奇怪,在这一句前面加了输出测试System.out.println(dao);发现dao为null;后面了解到原来是JUnit5中,初始化dao出错了,不应该用@Before,而应该用@BeforeAll,并且通过看@BeforeAll源码可以了解到该方法必须为静态,这就是JUnit5和JUnit4的区别之一。
所有我改掉之后
//UserDao dao;
/@Before
public static void init() {
dao=new UserDao();
}
/
static UserDao dao;
@BeforeAll
public static void init() {
dao=new UserDao();
}
结果就出来了:
Hibernate:
select
user0_.ID as ID0_0_,
user0_.USERNAME as USERNAME0_0_,
user0_.PASSWORD as PASSWORD0_0_,
user0_.GENDER as GENDER0_0_,
user0_.AGE as AGE0_0_,
user0_.ADDRESS as ADDRESS0_0_
from
JAVATEST user0_
where
user0_.ID=?
com.qianfeng.model.User@2638011
1,XXX,123

你可能感兴趣的:(关于Hibernate中JUnit版本导致空指针异常问题)