为了账号安全,请及时绑定邮箱和手机立即绑定

我的junit4测试出错 !

import java.util.Date;


import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.boot.MetadataSources;

import org.hibernate.boot.registry.StandardServiceRegistry;

import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;


public class StudentsTest {

private SessionFactory sessionFactory;

private Session session;

@Before

public void init(){

// A SessionFactory is set up once for an application!

StandardServiceRegistry registry = new StandardServiceRegistryBuilder()

.configure() // configures settings from hibernate.cfg.xml

.build();

try {

sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();

}

catch (Exception e) {

// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory

// so destroy it manually.

StandardServiceRegistryBuilder.destroy( registry );

System.out.println("sessionFactory");

}

   

   session=sessionFactory.openSession();    

   session.beginTransaction();

   

}

@After

public void destory(){

session.getTransaction().commit();

session.close();

sessionFactory.close();

}

@Test

public void testSaveStudents(){

Students students=new Students(2,"张三丰","男",new Date(),"武当山");

session.save(students);

}


}


为什么我这个代码老是提示空指针异常呢?

提示session找不着!!


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

        <property name="hibernate.connection.password">123456</property>

        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>

        <property name="hibernate.connection.username">root</property>

         <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> 


        

        <property name="hibernate.show_sql">true</property>

        <property name="hibernate.format_sql">true</property>

        <property name="hibernate.hbm2ddl.auto">update</property>

          

        <mapping resource="Students.hbm.xml" />

        

        <mapping class="//Students.hbm.xml"/>

     

    </session-factory>

</hibernate-configuration>


<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- Generated 2017-2-20 15:36:28 by Hibernate Tools 3.5.0.Final -->

<hibernate-mapping>

    <class name="Students" table="STUDENTS">

        <id name="sid" type="int">

            <column name="SID" />

            <generator class="assigned" />

        </id>

        <property name="sname" type="java.lang.String">

            <column name="SNAME" />

        </property>

        <property name="gender" type="java.lang.String">

            <column name="GENDER" />

        </property>

        <property name="birthday" type="java.util.Date">

            <column name="BIRTHDAY" />

        </property>

        <property name="address" type="java.lang.String">

            <column name="ADDRESS" />

        </property>

        

    </class>

</hibernate-mapping>


import java.util.Date;


import javax.persistence.Entity;

import javax.persistence.Table;



@Entity

@Table( name = "Students" )

public class Students {


       private int sid;

       private String sname;

       private String gender;

  private Date birthday;

       private String address;

public Students(){

}



public Students(int sid, String sname, String gender, Date birthday, String address) {

this.sid = sid;

this.sname = sname;

this.gender = gender;

this.birthday = birthday;

this.address = address;

}



public int getSid() {

return sid;

}



public void setSid(int sid) {

this.sid = sid;

}



public String getSname() {

return sname;

}



public void setSname(String sname) {

this.sname = sname;

}



public String getGender() {

return gender;

}



public void setGender(String gender) {

this.gender = gender;

}



public Date getBirthday() {

return birthday;

}



public void setBirthday(Date birthday) {

this.birthday = birthday;

}



public String getAddress() {

return address;

}



public void setAddress(String address) {

this.address = address;

}



@Override

public String toString() {

return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday

+ ", address=" + address + "]";

}

}


正在回答

2 回答

我的版本是5.2的,我在hibernate官网上看到的这个穿件session的方法,但是不知道为什么会出错 

0 回复 有任何疑惑可以回复我~

是不是因为hibernate版本高了,那个创建session语句不对

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Hibernate初探之单表映射
  • 参与学习       74810    人
  • 解答问题       793    个

Java持久化框架Hibernate入门教程,掌握Hibernate基本概念

进入课程

我的junit4测试出错 !

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信