首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

hibernate总是报空指针是咋回事

2012-12-17 
hibernate总是报空指针是怎么回事public class Student implements java.io.Serializable{private int id

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>


INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
2012-11-8 16:00:30 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.6.Final}
2012-11-8 16:00:30 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
2012-11-8 16:00:30 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
2012-11-8 16:00:30 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
2012-11-8 16:00:30 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
2012-11-8 16:00:30 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
2012-11-8 16:00:30 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: entity/Student.hbm.xml
2012-11-8 16:00:30 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
2012-11-8 16:00:30 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
2012-11-8 16:00:30 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
2012-11-8 16:00:30 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
2012-11-8 16:00:30 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
2012-11-8 16:00:30 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thin:@localhost:1521:orcl]
2012-11-8 16:00:30 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=zhxl, password=****}
2012-11-8 16:00:30 org.hibernate.engine.jdbc.internal.JdbcServicesImpl configure
WARN: HHH000341: Could not obtain connection metadata : 不支持的特性
2012-11-8 16:00:30 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000422: Disabling contextual LOB creation as connection was null
Exception in thread "main" java.lang.ExceptionInInitializerError
at test.Test.main(Test.java:15)
Caused by: java.lang.NullPointerException
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:207)
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)


at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:73)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2279)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2275)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
at dao.Dao.buildSessionFactory(Dao.java:19)
at dao.Dao.<clinit>(Dao.java:11)
... 1 more
[解决办法]
连没连接到数据库哦?
创建的连接是无效的!
检查数据库是否启动、或者说是不是JDBC的驱动jar导错了?
[解决办法]
jdbc oracle的驱动有没有

热点排行
Bad Request.