简体   繁体   English

Hibernate 4.3.0.Final获取会话

[英]Hibernate 4.3.0.Final get session

In the hibernate documentation for version 4.3.0.Final the following code snippet is given to create a SessionFactory : 版本4.3.0.Finalhibernate文档中,给出了以下代码片段来创建SessionFactory

package org.hibernate.tutorial.util;

import org.hibernate.SessionFactory;
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();
        }
        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;
    }

}

This seems to be outdated, as the method buildSessionFactory() is deprecated. 这似乎已经过时了,因为不推荐使用方法buildSessionFactory() What is the correct way to create the SessionFactory ? 创建SessionFactory的正确方法是什么?

public class TestHB4 {
    private static StandardServiceRegistry serviceRegistry;
    private static SessionFactory sessionFactory;

    public static void main(String[] args) {
        Person person = new Person();
        person.setFirstName("Namal");
        person.setLastName("Dinesh");


        Configuration configuration = new Configuration().configure();
        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        sessionFactory = configuration.configure().buildSessionFactory(serviceRegistry);

        Session session = sessionFactory.openSession();

        session.beginTransaction();

        session.save(person);

        session.getTransaction().commit();
        session.close();


    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在hibernate-validator 4.3.0.Final中使用会话方法load() - How to use session method load() in the hibernate-validator 4.3.0.Final Hibernate 4.3.0.Final和Spring Data JPA 1.4.3.RELEASE - Hibernate 4.3.0.Final & Spring Data JPA 1.4.3.RELEASE 使用Hibernate-entitymanager 4.3.0.Final in Play 2.2.1项目? - Using hibernate-entitymanager 4.3.0.Final in Play 2.2.1 project? Hibernate 4.3.0 Final和Spring 4.1 - Hibernate 4.3.0 final and spring 4.1 Spring 4.3.0.RELEASE + Hibernate 5.2.0.Final-无法获得当前线程的事务同步会话 - Spring 4.3.0.RELEASE + Hibernate 5.2.0.Final - Could not obtain transaction-synchronized Session for current thread 从 Eclipse Luna 卸载 JBoss Tools 4.3.0 Final (Hibernate) - Uninstall JBoss Tools 4.3.0 Final (Hibernate) from Eclipse Luna Spring 4.3.0.RELEASE + Hibernate 5.2.0.Final - mysql上的GeneratedValue - Spring 4.3.0.RELEASE + Hibernate 5.2.0.Final - GeneratedValue on mysql Spring 4.3.0.RELEASE + Hibernate 5.2.0.Final + JPA Repository - Spring 4.3.0.RELEASE + Hibernate 5.2.0.Final + JPA Repository 如何在Hibernate 4.3.4.Final中配置和获取会话? - How to configure and get session in Hibernate 4.3.4.Final? Spring 4.3.0.RELEASE + Hibernate 5.2.0.Final -Error .. with root cause org.hibernate.PersistentObjectException - Spring 4.3.0.RELEASE + Hibernate 5.2.0.Final -Error.. with root cause org.hibernate.PersistentObjectException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM