hibernate4.3.8的dialect和创建SessionFactory遇到的一些问题

好久不用hibernat,心里记着的还是hibernate3的标准,今天换成hibernate4.3.8后问题层出不穷啊。。。

首先是hibernate4.3.8中使用mysql方言时,hibernate.cfg.xml中配置的是

<property name="dialect">org.hibernate.dialect.MySQL57InnoDBDialect</property>

其次,在创建SessionFactory时废了不少的功夫,以前的buildSessionFactory()方法过时了,

同时在使用注解时也不能再new AnnotationConfiguration()

查了一番文档后发现建立SessionFactory改用以下代码

 1 Configuration cfg = new Configuration();
 2         cfg.configure();
 3         ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();//这里改为build()方法
 4         SessionFactory sf = cfg.buildSessionFactory(serviceRegistry);
 5         Session session = sf.openSession();
 6         session.beginTransaction();
 7         session.save(student);
 8         session.getTransaction().commit();
 9         session.close();
10         sf.close();

同时,无论是使用xml文件还是注释,现在直接用这套代码都可以创建SessionFactory了,以前的AnnotationConfiguration类

被包含进了Configuration类中!

时间: 2024-08-26 01:42:04

hibernate4.3.8的dialect和创建SessionFactory遇到的一些问题的相关文章

[Hibernate系列—] 2. 创建SessionFactory 与 Session

Configuration 对象创建 要创建SessionFactory , 首先要创建Configuration 对象. 这个对象就是去读取hibernate 的一些配置信息. 默认状况下, hibernate会到 classPath 目录下加载hibernate.cfg.xml 文件. 这里延续上一篇的例子: [Hibernate系列-] 1. 下载与试用Hibernate(MySQL与Oracle 配置) 在Eclipse 中进行开发. 这个配置文件的方式可以有多种, 可以是xml , 可

实例演示如何在spring4.2.2中集成hibernate5.0.2并创建sessionFactory

本文地址:http://blog.csdn.net/sushengmiyan/article/details/49388209 文章作者:苏生米沿 本文目的:使用spring4.2.2集成hibernate5.0.2并创建sessionFactory实例. 开发环境:eclipse(jee_mars) JDK1.8 MYSQL5.6 spring下载地址: https://repo.spring.io/list/release/org/springframework/spring/4.2.2.R

hibernate配置之&lt;property name=&quot;hbm2ddl.auto&quot;&gt;create&lt;/property&gt;导致每次创建SessionFactory都清空数据库中的数据

参考:http://stackoverflow.com/questions/6611437/how-to-make-hibernate-not-drop-tables 我遇到的问题就是: List l = sess.createCriteria(News.class) .add( Restrictions.isNotEmpty("title")) .list(); 抛出异常,Exception in thread "main" org.hibernate.Mappi

org.hibernate.InvalidMappingException: Could not parse mapping document from无法创建sessionFactory

把 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" 改为 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 原文地址:https://www.cnblogs.com/zhaocundang/p/9922005.html

Spring学习(五)spring整合hibernate

上一篇博客中讲到spring dao层对jdbc的封装,用到了模板模式的设计思想 .这篇我们来看看spring中的orm层对hibernate的封装,也就是所谓的spring整合 hibernate.这里同样用了模板模式, 将hibernate开发流程封装在ORM层提供的模板类HibernateTemplate中,通过在DAO中对模板类的使用,实现对传统hibernate开发流程的代替. 一.先来看看Hibernate的传统开发流程: 1) 配置SessionFactory对象 hibernat

Spring框架学习(4)spring整合hibernate

内容源自:spring整合hibernate    spring整合注解形式的hibernate 这里和上一部分学习一样用了模板模式, 将hibernate开发流程封装在ORM层提供的模板类HibernateTemplate中,通过在DAO中对模板类的使用,实现对传统hibernate开发流程的代替. 一.先来看看Hibernate的传统开发流程: 1) 配置SessionFactory对象 hibernate.cfg.xml <session-factory> a 数据源 driver_cl

sessionFactory的创建和四种查询方式

1,关于sessionFactory的创建 5.0版本之前,下面这种方式在5.0及之后,可能会出问题,建议修改为5.0之后的方式 // 实例化Configuration Configuration configuration = new Configuration(); // 加载hibernate.cfg.xml文件 configuration.configure("/hibernate.cfg.xml"); // 创建SessionFactory StandardServiceRe

SessionFactory的创建和Session的获得

1.当我们调用 Configuration config=new Configuration().configure(); 时候Hibernate会自己主动在当前的CLASSPATH中搜寻hibernate.cfg.xml文件并将其读取到内存作为后继操作的基础配置. 我们也能够指定配置文件名称,假设不希望使用默认的hibernate.cfg.xml文件作为配置文件的话: SessionFactory SessionFactory 负责创建Session的实例.我们能够通过Configuation

Hibernate SessionFactory的创建和session的获取

1,当我们调用 Configuration config=new Configuration().configure(); 时候Hibernate会自动在当前的CLASSPATH中搜寻hibernate.cfg.xml文件并将其读取到内存作为后继操作的基础配置. 我们也可以指定配置文件名,如果不希望使用默认的hibernate.cfg.xml文件作为配置文件的话: SessionFactory SessionFactory 负责创建Session的实例.我们可以通过Configuation实例创