初学hibernate遇到的关于HibernateUtil的问题

J2EE 码拜 9年前 (2015-04-08) 1353次浏览 0个评论

官方文档提供的HibernateUtil是这样的

package org.hibernate.tutorial.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            //明显有语法错误,我在下面加了一句return
            new Configuration().configure().buildSessionFactory(
			    new StandardServiceRegistryBuilder().build() );
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

用这个报Access to DialectResolutionInfo cannot be null when “”hibernate.dialect”” not这个错误,然后在网上找到的解决方案改成下面这样,就没问题了

package com.example.util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
        
        	Configuration cfg = new Configuration().configure();
        	ServiceRegistry serviceRegistry= new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
        	return cfg.buildSessionFactory(serviceRegistry);
//           return  new Configuration().configure().buildSessionFactory(
//			    new StandardServiceRegistryBuilder().build() );
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

到底是信官方的还是…..还有后面那个方法报了好几个deprecated的警告
初学真是越学越奇怪啊。。

初学hibernate遇到的关于HibernateUtil的问题
5分
我也是刚学完hibernate   这是我用的hibernateUtil  没有报什么错   
不过确实看不太懂。。会用就像

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

public class HibernateUtil {

    private static final SessionFactory sessionFactory;
    
    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

初学hibernate遇到的关于HibernateUtil的问题
[b][b][/b][/b]

引用 1 楼 mwb631714470 的回复:

我也是刚学完hibernate   这是我用的hibernateUtil  没有报什么错   
不过确实看不太懂。。会用就像

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

public class HibernateUtil {

    private static final SessionFactory sessionFactory;
    
    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

恩 这个好像也有方法是deprecated,先学再说吧。。

初学hibernate遇到的关于HibernateUtil的问题
还请大家给初学者一些建议和经验啊。。。
初学hibernate遇到的关于HibernateUtil的问题
15分
你用的是哪个版本呀,如果是4的话用后者,用前者会报错好像是一个bug,如果是3的话那么用前者即可
初学hibernate遇到的关于HibernateUtil的问题
5分
hibernate 3和4版本是不同的,前一个是3的,后一个是4的写法吧
初学hibernate遇到的关于HibernateUtil的问题
5分
楼主改的并没有错,buildSessionFactory需要返回一个值但官方的没给,改成你那样是对的,kang2618在静态代码块里给sessionFactory赋值也是可以的
初学hibernate遇到的关于HibernateUtil的问题
引用 4 楼 zy353003874 的回复:

你用的是哪个版本呀,如果是4的话用后者,用前者会报错好像是一个bug,如果是3的话那么用前者即可

嗯,用的是4.3.6。

初学hibernate遇到的关于HibernateUtil的问题
引用 5 楼 u010779899 的回复:

hibernate 3和4版本是不同的,前一个是3的,后一个是4的写法吧

嗯,用的是4.3.6

初学hibernate遇到的关于HibernateUtil的问题
引用 6 楼 zhoulenihao 的回复:

楼主改的并没有错,buildSessionFactory需要返回一个值但官方的没给,改成你那样是对的,kang2618在静态代码块里给sessionFactory赋值也是可以的

初学hibernate遇到的关于HibernateUtil的问题
5分
使用后一种写法吧,这个问题当初stackoverflow中有人提出过
初学hibernate遇到的关于HibernateUtil的问题
5分
引用 1 楼 mwb631714470 的回复:

我也是刚学完hibernate   这是我用的hibernateUtil  没有报什么错   
不过确实看不太懂。。会用就像

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

public class HibernateUtil {

    private static final SessionFactory sessionFactory;
    
    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

估计你得hibernate版本比较老了


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明初学hibernate遇到的关于HibernateUtil的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!