当前位置: 代码迷 >> Java Web开发 >> hibernate总是报空指针是咋回事
  详细解决方案

hibernate总是报空指针是咋回事

热度:7538   发布时间:2013-02-25 21:08:11.0
hibernate总是报空指针是怎么回事
public class Student implements java.io.Serializable{
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}


public class Dao {
private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {

Configuration cfg = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(cfg.getProperties()).buildServiceRegistry();

return cfg.buildSessionFactory(serviceRegistry);


}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}


public class Test {

/**
 * @param args
 */
public static void main(String[] args) {
Session session = Dao.getSessionFactory().openSession();


Transaction tx = session.beginTransaction();


Student s=new Student();
s.setId(2);
s.setName("admin");
session.save(s);

tx.commit();

session.close();
}

}



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.tutorial.domain">
<class name="entity.Student" table="student">
<id name="id"></id>
<property name="name"></property>
</class>
</hibernate-mapping>


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">zhxl</property>
<property name="connection.password">zhxl</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="entity/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>


不知道为什么总报空指针

2012-11-8 16:00:30 org.hibernate.annotations.common.Version <clinit>
  相关解决方案