hibernate.cfg.xml

文件名:hibernate.cfg.xml

文件位置:src目录下

核心文件内容说明

<!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>
            <!--链接数据库基础 必须-->
            <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate?useSSL = FALSE &amp; serverTimezone = UTC</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password">root</property>

            <!--数据库方言  必须-->
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

            <!--数据库默认模式  修改(无表结构则新增表结构,有就修改)-->
            <property name="hbm2ddl.auto">update</property>

            <!--显示sql语句-->
            <property name="show_sql">true</property>

            <!--sql语句格式化-->
            <property name="format_sql">true</property>

            <!--将session绑定到当前线程中,获得getCurrentSession-->
            <property name="hibernate.current_session_context_class">thread</property>


            <!--加载配置文件 必须-->
            <mapping resource="com/hibernate/entity/User.hbm.xml" ></mapping>
        </session-factory>
</hibernate-configuration>

可能会出现的错误

错误提示:

org.hibernate.HibernateException: Calling method 'save' is not valid without an active transaction (Current status: NOT_ACTIVE)

org.hibernate.HibernateException: createSQLQuery is not valid without active transaction

原因:获取到的session不是同一个

工具类(HibernateUtils):

package com.hibernate.utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class HibernateUtils {
    private static  final Configuration CONFIGURATION;
    private  static final SessionFactory SESSION_FACTORY;

    static {
        CONFIGURATION = new Configuration();
        CONFIGURATION.configure();
        SESSION_FACTORY = CONFIGURATION.buildSessionFactory();
    }

    //单独开启一个session
    public static Session getSession(){
        return SESSION_FACTORY.openSession();
    }

    //使用getCurrentSession获取的session是绑定在线程上的
    public static Session getCurrentSession(){
        return SESSION_FACTORY.getCurrentSession();
    } 
}

注意:上面有两个获取session的方法

1.直接开启一个session (getSession())

2.从线程中获取一个session (getCurrentSession())

<!--将session绑定到当前线程中,获得getCurrentSession-->
<property name="hibernate.current_session_context_class">thread</property>

service层类(UserServiceImpl):

package com.hibernate.service.impl;

import com.hibernate.dao.UserDao;
import com.hibernate.entity.User;
import com.hibernate.service.UserService;
import com.hibernate.utils.BeanFactory;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class UserServiceImpl implements UserService {
    @Override
    public void addUser() {
        //开启事务
        Session session = HibernateUtils.getSession();
        Transaction tr = session.beginTransaction();
        try {
            //开始添加用户
            UserDao dao = (UserDao) BeanFactory.getBeanFactory("UserDao");
            User user = new User("胖子", 18);
            dao.add(user);
            int i = 10/ 0;
            User user2 = new User("哈哈", 18);
            dao.add(user2);
            tr.commit();
        } catch (Exception e) {
            e.printStackTrace();
            tr.rollback();
        }
    }
}

这里session获取是用的getSession(),

也就是重新开启的一个session,

并没有从线程中去获取

Dao层的类(UserDaoImpl):

package com.hibernate.dao.impl;

import com.hibernate.dao.UserDao;
import com.hibernate.entity.User;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.Session;

public class UserDaoImpl implements UserDao {

    @Override
    public void add(User user) {
        Session cs = HibernateUtils.getCurrentSession();
        cs.save(user);
    }
}

这里获取session使用的是getCurrentSession()

获取的是线程中的session,

所以和上面service层的session不是同一个

提示:

在spring框架中会帮我们代理这个session,所以我们不需要配置

<property name="hibernate.current_session_context_class">thread</property>

否则会报第二个错误

参考博客网址

posted on 2020-11-22 22:30  人生之外的路途  阅读(261)  评论(0编辑  收藏  举报