Hibernate 4.1.1的第一個例子HelloWorld – JAVA編程語言程序開發技術文章

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裡面可以找到

[html] 
<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
 
<hibernate-configuration> 
 
    <session-factory> 
 
        <!– Database connection settings –> 
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property> 
        <property name="connection.username">sa</property> 
        <property name="connection.password">sa</property> 
 
        <!– JDBC connection pool (use the built-in) 
        <property name="connection.pool_size">1</property> 
         –> 
        <!– SQL dialect –> 
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 
 
        <!– Enable Hibernate's automatic session context management  
        <property name="current_session_context_class">thread</property> 
        –> 
         
        <!– Disable the second-level cache  –> 
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> 
 
        <!– Echo all executed SQL to stdout –> 
        <property name="show_sql">true</property> 
 
        <!– Drop and re-create the database schema on startup 
        <property name="hbm2ddl.auto">update</property> 
         –> 
          
     <!–   <mapping resource="com/bird/model/Student.hbm.xml"/> –>  
        <mapping class="com.bird.model.Teacher"/> 
    </session-factory> 
 
</hibernate-configuration> 

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!– Database connection settings –>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">sa</property>
        <property name="connection.password">sa</property>

        <!– JDBC connection pool (use the built-in)
        <property name="connection.pool_size">1</property>
   –>
        <!– SQL dialect –>
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <!– Enable Hibernate's automatic session context management
        <property name="current_session_context_class">thread</property>
  –>
  
        <!– Disable the second-level cache  –>
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!– Echo all executed SQL to stdout –>
        <property name="show_sql">true</property>

        <!– Drop and re-create the database schema on startup
        <property name="hbm2ddl.auto">update</property>
   –>
  
     <!–   <mapping resource="com/bird/model/Student.hbm.xml"/> –>
  <mapping class="com.bird.model.Teacher"/>
    </session-factory>

</hibernate-configuration>

然後是具體的類

[java] 
package com.bird.model; 
 
import javax.persistence.Entity; 
import javax.persistence.Id; 
 
@Entity 
public class Teacher { 
 
    private int id; 
    private String name; 
    private String title; 
 
    @Id 
    public int getId() { 
        return id; 
    } 
 
    public void setId(int id) { 
        this.id = id; 
    } 
 
    public String getName() { 
        return name; 
    } 
 
    public void setName(String name) { 
        this.name = name; 
    } 
 
    public String getTitle() { 
        return title; 
    } 
 
    public void setTitle(String title) { 
        this.title = title; 
    } 
 

package com.bird.model;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Teacher {

 private int id;
 private String name;
 private String title;

 @Id
 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

}

其中的表名和類名相同,其他的字段和Bean的屬相相同。

 

最後是使用他

[java] 
package com.bird.test; 
 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.service.ServiceRegistry; 
import org.hibernate.service.ServiceRegistryBuilder; 
 
import com.bird.model.Teacher; 
 
 
public class TeacherTest { 
 
    /**
     * @param args
     */ 
    public static void main(String[] args) { 
        Teacher t = new Teacher(); 
        t.setId(1); 
        t.setName("t1"); 
        t.setTitle("中級"); 
         
        Configuration cfg = new Configuration(); 
        cfg.configure();//讀取配置文件  
         
        ServiceRegistry serviceRegistry =new ServiceRegistryBuilder(). 
        applySettings(cfg.getProperties()).buildServiceRegistry(); 
         
        SessionFactory factory = cfg.configure().buildSessionFactory(serviceRegistry); 
         
        Session session = factory.openSession(); 
        session.beginTransaction(); 
        session.save(t); 
        session.getTransaction().commit(); 
        session.close(); 
        factory.close(); 
    } 
 

package com.bird.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import com.bird.model.Teacher;

public class TeacherTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  Teacher t = new Teacher();
  t.setId(1);
  t.setName("t1");
  t.setTitle("中級");
  
  Configuration cfg = new Configuration();
  cfg.configure();//讀取配置文件
  www.aiwalls.com
  ServiceRegistry serviceRegistry =new ServiceRegistryBuilder().
  applySettings(cfg.getProperties()).buildServiceRegistry();
  
  SessionFactory factory = cfg.configure().buildSessionFactory(serviceRegistry);
  
  Session session = factory.openSession();
  session.beginTransaction();
  session.save(t);
  session.getTransaction().commit();
  session.close();
  factory.close();
 }

}

這樣基於最新的hibernate4.1.1的helloworld就OK瞭
作者:a352193394
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *