Hibernate 4.0与之前的3.X版本改进

来源:互联网 发布:横版格斗手游源码 编辑:程序博客网 时间:2024/04/27 21:15

Hibernate 4.0与之前的3.X版本改进很很多,下面先将改动的地方说一下。

1.数据库方言设置

<property name=”dialect”>org.hibernate.dialect.MySQL5Dialect</property>

在3.3版本中连接MySQL数据库只需要指明MySQLDialect即可。在4.1版本中可以指出MySQL5Dialect

2.buildSessionFactory

4.1版本中buildSessionFactory()已经被buildSessionFactory(ServiceRegistry ServiceRegistry)取代

解决办法:

Configuration cfg = new Configuration();

cfg.configure();

ServiceRegistry serviceRegistry =new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();

SessionFactory sf = cfg.configure().buildSessionFactory(serviceRegistry);

3.annotation

org.hibernate.cfg.AnnotationConfiguration;

Deprecated. All functionality has been moved to Configuration

这个注解读取配置的class已经废弃,现在读取配置不需要特别注明是注解,直接用Configuration cfg = new Configuration();就可以读取注解。

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

由于Hibernate推荐使用注解,所以基于hbm的配置文件我们就不写了,而且对于新的server读取配置文件的方法建立session对于配置文件的读取貌似也有问题,我测试了好几个都没办法解决,所以这里先只介绍一下基于注解的方法了。

首先是配置文件,这个在hibernate的mannual里面可以找到

  1. <?xml version='1.0' encoding='utf-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
  5.   
  6. <hibernate-configuration>  
  7.   
  8.     <session-factory>  
  9.   
  10.         <!-- Database connection settings -->  
  11.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  12.         <property name="connection.url">jdbc:mysql://localhost:3306/test</property>  
  13.         <property name="connection.username">sa</property>  
  14.         <property name="connection.password">sa</property>  
  15.   
  16.         <!-- JDBC connection pool (use the built-in)  
  17.         <property name="connection.pool_size">1</property>  
  18.          -->  
  19.         <!-- SQL dialect -->  
  20.         <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>  
  21.   
  22.         <!-- Enable Hibernate's automatic session context management   
  23.         <property name="current_session_context_class">thread</property>  
  24.         -->  
  25.           
  26.         <!-- Disable the second-level cache  -->  
  27.         <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>  
  28.   
  29.         <!-- Echo all executed SQL to stdout -->  
  30.         <property name="show_sql">true</property>  
  31.   
  32.         <!-- Drop and re-create the database schema on startup  
  33.         <property name="hbm2ddl.auto">update</property>  
  34.          -->  
  35.            
  36.      <!--   <mapping resource="com/bird/model/Student.hbm.xml"/> -->   
  37.         <mapping class="com.bird.model.Teacher"/>  
  38.     </session-factory>  
  39.   
  40. </hibernate-configuration>  
  41. 然后是具体的类

    1. package com.bird.model;  
    2.   
    3. import javax.persistence.Entity;  
    4. import javax.persistence.Id;  
    5.   
    6. @Entity  
    7. public class Teacher {  
    8.   
    9.     private int id;  
    10.     private String name;  
    11.     private String title;  
    12.   
    13.     @Id  
    14.     public int getId() {  
    15.         return id;  
    16.     }  
    17.   
    18.     public void setId(int id) {  
    19.         this.id = id;  
    20.     }  
    21.   
    22.     public String getName() {  
    23.         return name;  
    24.     }  
    25.   
    26.     public void setName(String name) {  
    27.         this.name = name;  
    28.     }  
    29.   
    30.     public String getTitle() {  
    31.         return title;  
    32.     }  
    33.   
    34.     public void setTitle(String title) {  
    35.         this.title = title;  
    36.     }  
    37.   
    38. }  

    其中的表名和类名相同,其他的字段和Bean的属相相同。

    最后是使用他

    1. package com.bird.test;  
    2.   
    3. import org.hibernate.Session;  
    4. import org.hibernate.SessionFactory;  
    5. import org.hibernate.cfg.Configuration;  
    6. import org.hibernate.service.ServiceRegistry;  
    7. import org.hibernate.service.ServiceRegistryBuilder;  
    8.   
    9. import com.bird.model.Teacher;  
    10.   
    11.   
    12. public class TeacherTest {  
    13.   
    14.     /** 
    15.      * @param args 
    16.      */  
    17.     public static void main(String[] args) {  
    18.         Teacher t = new Teacher();  
    19.         t.setId(1);  
    20.         t.setName("t1");  
    21.         t.setTitle("中级");  
    22.           
    23.         Configuration cfg = new Configuration();  
    24.         cfg.configure();//读取配置文件   
    25.           
    26.         ServiceRegistry serviceRegistry =new ServiceRegistryBuilder().  
    27.         applySettings(cfg.getProperties()).buildServiceRegistry();  
    28.           
    29.         SessionFactory factory = cfg.configure().buildSessionFactory(serviceRegistry);  
    30.           
    31.         Session session = factory.openSession();  
    32.         session.beginTransaction();  
    33.         session.save(t);  
    34.         session.getTransaction().commit();  
    35.         session.close();  
    36.         factory.close();  
    37.     }  
    38.   
    39. }  

    这样基于最新的hibernate4.1.1的helloworld就OK了。