hibernate4 使用及 新特性

hibernate4.x已经在官网出现一段时间了.
下载地址: http://hibernate.org/orm/downloads/

使用hibernate4所需要的jar包 在lib equired(必选的), 该文件夹中的jar必选都要copy,其他文件夹的可以有选择性的copy; 

1.buildSessionFactory

Configuration config = new Configuration();//
        SessionFactory factory = config.buildSessionFactory();

被下面退换,否则引入包会报错 org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

Configuration cfg = new Configuration().configure();
         @SuppressWarnings("deprecation")
         ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
         .applySettings(cfg.getProperties()).buildServiceRegistry();
         SessionFactory factory = cfg.buildSessionFactory(serviceRegistry);

2.annotation注解

org.hibernate.cfg.AnnotationConfiguration;
Deprecated. All functionality has been moved to Configuration
这个注解读取配置的class已经废弃,现在读取配置不需要特别注明是注解,直接用Configuration cfg = new Configuration();就可以读取注解。

Hibernate4.X版本中推荐使用annotation配置,所以在引进jar包时把requested里面的包全部引进来就已经包含了annotation必须包了

3.自动建表

 Configuration cft = new Configuration().configure();
        SchemaExport export = new SchemaExport(cft);
        export.create(true, true);

4.数据库方言设置

<property name=”dialect”>org.hibernate.dialect.MySQL5Dialect</property>
在3.3版本中连接MySQL数据库只需要指明MySQLDialect即可。在4.1版本中可以指出MySQL5Dialect
原文地址:https://www.cnblogs.com/zhao123/p/4009661.html