你的位置:首页 > Java教程

[Java教程]我的Hibernate入门


  今天忙了一整天,终于搭建好了我的第一个Hibernate程序,中间关于hibernate.cfg.

  首先在你的eclipse中安装Hibernate Tools插件方便创建cfg.cml与hbm.

奥添加yi

当然在最前面还要添加hibernate jar包,musql driver等,由于我使用maven管理项目,因此直接在maven的pom文件中添加就可以了。

在hibernate.cfg.

 1 <??> 2 <!DOCTYPE hibernate-configuration PUBLIC 3     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6   <session-factory > 7     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 8     <property name="hibernate.connection.password">admin</property> 9     <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?characterEncoding=utf-8</property>10     <property name="hibernate.connection.username">root</property>11     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>12     <!-- 在控制台输出SQL语句 -->13     <property name="show_sql">true</property>14     <!-- Hibernate启动时自动创建表结构 -->15     <property name="hbm2ddl.auto">create</property>16     <!-- 不加 可能出现异常 -->17     <property name="current_sesson_context_class">thread</property>18     <!-- 指定Cat类为Hibernate实体类 -->19     <mapping resource="config/Cat.hbm./>20   </session-factory>21 </hibernate-configuration>

第九行url后面的 hibernate?characterEncoding=utf-8 是hibernate要操作的数据库名称,需要你自己先创建:

create database hibernate character set 'utf8'

19行Cat类是需要你自己创建的POJO实体类,实体类(Entity)是指与数据库有映射关系的Java类使用@Entity后Cat就被申明为了一个实体类。实体类还需配置对应的表名(@Table),主键(@Id),普通属性(@Column)对应列名等。

 1 package com.lxiao.model; 2  3  4 import java.util.Date; 5  6 import javax.persistence.Column; 7 import javax.persistence.Entity; 8 import javax.persistence.GeneratedValue; 9 import javax.persistence.GenerationType; 10 import javax.persistence.Id; 11 import javax.persistence.JoinColumn; 12 import javax.persistence.ManyToOne; 13 import javax.persistence.Table; 14 import javax.persistence.Temporal; 15 import javax.persistence.TemporalType; 16  17 @Entity 18 @Table(name="tb_cat") 19 public class Cat { 20  21   @Id 22   @GeneratedValue(strategy = GenerationType.AUTO) 23   private Integer Id; 24    25   @Column(name = "name") 26   private String name; 27    28   @Column(name = "description") 29   private String description; 30    31   @Column(name = "age") 32   private Integer age; 33    34   @Column(name="sex") 35   private String sex; 36    37   @ManyToOne 38   @JoinColumn(name = "mother_id") 39   private Cat mother; 40    41   @Temporal(TemporalType.TIMESTAMP) 42   @Column(name = "createDate") 43   private Date createDate; 44  45   public Integer getId() { 46     return Id; 47   } 48  49   public void setId(Integer id) { 50     Id = id; 51   } 52  53   public String getName() { 54     return name; 55   } 56  57   public void setName(String name) { 58     this.name = name; 59   } 60  61   public String getDescription() { 62     return description; 63   } 64  65   public void setDescription(String description) { 66     this.description = description; 67   } 68  69   public Integer getAge() { 70     return age; 71   } 72  73   public void setAge(Integer age) { 74     this.age = age; 75   } 76  77   public String getSex() { 78     return sex; 79   } 80  81   public void setSex(String sex) { 82     this.sex = sex; 83   } 84  85   public Cat getMother() { 86     return mother; 87   } 88  89   public void setMother(Cat mother) { 90     this.mother = mother; 91   } 92  93   public Date getCreateDate() { 94     return createDate; 95   } 96  97   public void setCreateDate(Date createDate) { 98     this.createDate = createDate; 99   }100 101 }

值得一提的是,该POJO类在定义玩le私有变量后,可以用eclipse自动生成getter,setter方法,免去了手写的枯燥。

然后创建Cat.hbm.

然后一路next就可以了,最后就在hibernte.cfg.

然后我们写一个HibernateUtil类来加载config文件hibernate.cfg.

 1 package com.lxiao.hibernate; 2  3 import org.hibernate.SessionFactory; 4 import org.hibernate.cfg.Configuration; 5  6 public class HibernateUtil { 7   private static final SessionFactory factory; 8    9   static{10     try{11       factory = new Configuration().configure("config/hibernate.cfg.).buildSessionFactory();12     }catch(Throwable e){13       System.out.println("Initial sesionFactory failed..."+e);14       throw new ExceptionInInitializerError(e);15     }16   }17   18   public static SessionFactory getSessionFactory(){19     return factory;20   }21 }