亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

我的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找不著?。?/p>


<?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="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 + "]";

}

}


正在回答

2 回答

我的版本是5.2的,我在hibernate官網上看到的這個穿件session的方法,但是不知道為什么會出錯?

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

是不是因為hibernate版本高了,那個創建session語句不對

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

舉報

0/150
提交
取消
Hibernate初探之單表映射
  • 參與學習       74801    人
  • 解答問題       835    個

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

進入課程

我的junit4測試出錯 !

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號