2 sjyioipuyt sjyioipuyt 于 2013.12.12 18:11 提问

一个hibernate连接oracle11g的问题

<?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">


oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@localhost:1521:ORCL
scott
tiger
org.hibernate.dialect.Oracle10gDialect

    <!-- Database config setting -->
 <!--<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
    <property name="connection.username">root</property>
    <property name="connection.password">123</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>-->

    <!-- JDBC connection pool (use the built-in) -->
   <!--<property name="connection.pool_size">1</property>-->

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

    <!-- Enable Hibernate's automatic session context management -->
   <!--<property name="current_session_context_class">thread</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>
    <property name="format_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">create</property>


    <!-- 使用xml配置方法的需要对下面的resource进行配置-->
    <mapping resource="com/sjy/hibernate/model/Student.hbm.xml"/>
    <mapping class="com.sjy.hibernate.model.Teacher"/>

    <!-- 使用annotation的需要对下面的class进行配置-->
   <!--<mapping class="org.hibernate.tutorial.domain.Event.hbm.xml" />-->
</session-factory>

这个是我的hibernate配置文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">









这个是类的配置文件

package com.sjy.hibernate.model;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class StudentTest {
private static SessionFactory sf;
@BeforeClass
public static void beforeClass(){

Configuration cfg = new Configuration();
cfg.configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
sf = cfg.buildSessionFactory(sr);
}
@Test
public void studentTest(){

Student s = new Student();
s.setAge(2);
s.setName("hello");
Session session = sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
}
@AfterClass
public static void afterClass(){
sf.close();
}

}

这个是我的测试代码

package com.sjy.hibernate.model;

public class Student {
private int id;
private String name;
private int age;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

这个是student这个类

我出的问题是连接oracle11g的时候老出空指针错误而且它里面报的是sf = cfg.buildSessionFactory(sr);sf 的空指针错误求告说解答下这个是什么错误啊
我这个配置换成mysql是好的能建表和插入数据但是换成oracle就成了空指针错误还有我的oracle里面导入的是classes12.jar 和ojdbc1.4jar这个两个包但是我认为不是导入jar包的问题

1个回答

ISEUWEI
ISEUWEI   2017.11.21 16:52

请问空指针的错误你最后是怎么解决的

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!