求助!hibernate报错 No TransactionManagerLookup specified 不知如何解决
江北厂青年 2017-05-22 11:55:51 log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
org.hibernate.HibernateException: No TransactionManagerLookup specified
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:503)
at common.HibernateSessionFactory.getSession(HibernateSessionFactory.java:26)
at biz.UserBiz.addNewUser(UserBiz.java:18)
at test.Test.main(Test.java:16)
菜鸟研究hibernate,跟着视频写,写完运行报错,网上找了好多都是说是这个----》<property name="current_session_context_class">thread</property> 问题, 搞了好久都没找出原因,请大神帮帮忙!!!不胜感激!下面是类和配置文件的代码
--------------HibernateSessionFactory类-----------
package common;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static Configuration cfg;
private static SessionFactory sessionFactory;
static {
try {
cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
} catch (HibernateException e) {
// TODO Auto-generated catch block
throw new RuntimeException("hibernate初始化失败", e);
}
}
public static Session getSession() {
return sessionFactory.getCurrentSession();
}
}
----------------------------UserBiz类-------------------------------------------
package biz;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;
import common.HibernateSessionFactory;
import po.User;
import dao.UserDao;
public class UserBiz {
private UserDao dao = new UserDao();
public void addNewUser(User user){
Transaction tx = null;
try {
tx = HibernateSessionFactory.getSession().beginTransaction();
dao.save(user);
tx.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(tx != null){
tx.rollback();
}
}
}
}
-----------------------------hibernate.cfg.xml配置文件 -----------------------------
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<!-- 创建连接 -->
<session-factory>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:ORACLE</property>
<property name="connection.username">system</property>
<property name="connection.password">orcl</property>
<!-- 辅助参数 -->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="current_session_context_class">thread</property>
<!-- 映射文件 -->
<mapping resource="po/User.hbm.xml" />
</session-factory>
</hibernate-configuration>