使用eclipse的hibernate连接oracle11g,导入的包如下

创建配置文件people.hbm.xml 在src下的com.example.entity包中
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.pansoft.entity">
<class name="People" table="t">
<id name = "id" column="id" type="int">
<generator class="native"></generator>
</id>
<property name="name" column="name" type = "string"></property>
</class>
</hibernate-mapping>
创建配置文件hibernate.cfg.xml 在src下
<?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>
<!-- 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">sys as sysdba</property>
<property name="connection.password">sys</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Names the annotated entity class -->
<mapping resource="com/example/entity/People.hbm.xml"/>
</session-factory>
</hibernate-configuration>
创建实体类people在src下的com.example.entity 包中
package com.example.entity;
public class People {
private int id;
private String name;
public People() {
// TODO 自动生成的构造函数存根
}
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;
}
}
创建hibernateutil类在com.example.entity包中
package com.example.entity;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class hibernateutil {
private static final SessionFactory session_Factory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
return new Configuration().configure().buildSessionFactory();
} catch (Exception e) {
// TODO: handle exception
System.err.println("session工厂创建失败"+e.getMessage());
throw new ExceptionInInitializerError(e.getMessage());
}
}
public static SessionFactory getSessionFactory() {
return session_Factory;
}
}
创建peoplemanage类在com.example.entity包中
package com.example.entity;
import org.hibernate.Hibernate;
import org.hibernate.Session;
public class PeopleManage {
public static void main(String[] args) {
PeopleManage peopleManage = new PeopleManage();
peopleManage.createAndStoreEvent(1, "王二");
}
private void createAndStoreEvent(int id,String name) {
Session session = hibernateutil.getSessionFactory().getCurrentSession();
session.beginTransaction();
People people = new People();
people.setId(id);
people.setName(name);
session.save(people);
session.getTransaction().commit();
}
}
运行出现如下问题:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:620)
at com.pansoft.entity.PeopleManage.createAndStoreEvent(PeopleManage.java:17)
at com.pansoft.entity.PeopleManage.main(PeopleManage.java:11)
------解决思路----------------------
缺少包:
apache commons-logging