sjyioipuyt 2013-12-12 10:11 采纳率: 0%
浏览 3881

一个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 2017-11-21 08:52
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题