我的junit4測(cè)試出錯(cuò) !
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(),"武當(dāng)山");
session.save(students);
}
}
為什么我這個(gè)代碼老是提示空指針異常呢?
提示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&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="http://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 + "]";
}
}
2017-02-21
我的版本是5.2的,我在hibernate官網(wǎng)上看到的這個(gè)穿件session的方法,但是不知道為什么會(huì)出錯(cuò)?
2017-02-21
是不是因?yàn)閔ibernate版本高了,那個(gè)創(chuàng)建session語(yǔ)句不對(duì)