且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

创建实体类使用Hibernate

更新时间:2022-01-27 05:35:55

 

   目前我们在使用Hibernate的时候往往先建立关系型数据表,这样的方式其实是没有正真体现Hibernate操作的内涵。

   合理的操作应该是建立实体类来生成关系型数据表,然后根据操作对象来操作数据表,这里的数据表对于开发者而言是绝对的透明的。

   Hibernate轻量级框架的使用就是为了提高开发效率,使用操作对象的方式来达到操作关系型数据库,正真的达到万物皆对象。

   下面是我采用手工的方式从正面使用Hibernate。

    1.创建Hibernate项目工程,即JavaWeb工程或者Java工程

    2.创建自定义类库,添加工程中要使用到的Jar包

     参见:自定义用户类库的方法 

   3.创建POJO即java实体类

     


  1. package xiao.zhang.hbean; 
  2.  
  3. import java.util.Date; 
  4.  
  5. public class Person { 
  6.  
  7.     private String perId; 
  8.     private String perName; 
  9.     private Date creatTime; 
  10.  
  11.     public Person() { 
  12.         super(); 
  13.     } 
  14.  
  15.     public Person(String perId, String perName, Date creatTime) { 
  16.         super(); 
  17.         this.perId = perId; 
  18.         this.perName = perName; 
  19.         this.creatTime = creatTime; 
  20.     } 
  21.  
  22.     //getter() setter() 

   4.创建实体类Person的配置文件,Person.hbm.xml

     放在实体类的目录下; 到Hibernate的实例工程中拷贝模版修改即可.

     


  1. <?xml version="1.0"?> 
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
  5. <hibernate-mapping package="org.hibernate.auction"> 
  6.  
  7.     <class name="xiao.zhang.hbean.Person" table="Person" lazy="true" schema="dbo" catalog="ext"> 
  8.         <id name="perId"> 
  9.             <generator class="uuid" /> 
  10.         </id> 
  11.         <property name="perName"> 
  12.             <column name="perNamee" length="20" ></column> 
  13.         </property> 
  14.         <property name="creatTime"> 
  15.             <column name="creatTime"></column> 
  16.         </property> 
  17.     </class> 
  18. </hibernate-mapping> 

 5.将Person.hbm.xml文件添加到hibernate.cfg.xml文件中的映射中。

   在hibernate.cfg.xml文件中配置数据库连接信息。

   


  1. <?xml version='1.0' encoding='UTF-8'?> 
  2. <!DOCTYPE hibernate-configuration PUBLIC 
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
  5.  
  6.     <!-- Generated by MyEclipse Hibernate Tools.   --> 
  7. <hibernate-configuration> 
  8.  
  9.     <session-factory> 
  10.         <property name="dialect"> 
  11.             org.hibernate.dialect.SQLServerDialect 
  12.     </property> 
  13.         <property name="hibernate.connection.username">sa</property> 
  14.         <property name="hibernate.connection.password">sa</property> 
  15.         <property name="hibernate.connection.url"> 
  16.             jdbc:sqlserver://localhost:1433;databaseName=ext 
  17.     </property> 
  18.         <property name="hibernate.connection.driver_class"> 
  19.             com.microsoft.sqlserver.jdbc.SQLServerDriver 
  20.     </property> 
  21.         <property name="hibernate.show_sql">true</property> 
  22.  
  23.         <mapping resource="xiao/zhang/hbean/Person.hbm.xml" /> 
  24.     </session-factory> 
  25.  
  26. </hibernate-configuration> 

注意手工配置:添加相应的数据库方言。

并且将Person.hbm.xml文件的资源路径添加到mapping映射中去。

6.编写一个将Person类生成为数据库表的类

 


  1. import org.hibernate.cfg.Configuration; 
  2. import org.hibernate.tool.hbm2ddl.SchemaExport; 
  3. public class HBCreateTable { 
  4.  
  5.     /** 
  6.      * @param args 
  7.      */ 
  8.     public static void main(String[] args) { 
  9.         Configuration   cfg=new Configuration().configure(); 
  10.          
  11.         SchemaExport sexport=new SchemaExport(cfg); 
  12.         sexport.create(truetrue); 
  13.     } 

Configuration().configure()默认加载hibernate.cfg.xml;如果有改动则需要在configure方法中传入资源路径字符串

运行后生成在数据库中生成Person表。生成数据库中的表名在Person.hbm.xml文件中定义。

程序执行的SQL语句如下:



  1. drop table ext.dbo.Person 
  2. create table ext.dbo.Person (perId varchar(255) not null, perNamee varchar(20) null, creatTime datetime nullprimary key (perId)) 

数据库中的情况如下图:


创建实体类使用Hibernate

7.编写程序,对对象进行操作。



  1. import java.util.Date; 
  2. import org.hibernate.SessionFactory; 
  3. import org.hibernate.cfg.Configuration; 
  4. import org.hibernate.Session; 
  5.  
  6. public class TestPeson { 
  7.  
  8.     /** 
  9.      * @param args 
  10.      */ 
  11.     public static void main(String[] args) { 
  12.         Configuration cfg = new Configuration().configure(); 
  13.         SessionFactory sf = cfg.buildSessionFactory(); 
  14.  
  15.         Session session = null
  16.         try { 
  17.             session = sf.openSession(); 
  18.             session.beginTransaction(); 
  19.             //**************************// 
  20.              
  21.              
  22.             //*************************// 
  23.             session.getTransaction().commit(); 
  24.  
  25.         } catch (Exception e) { 
  26.             session.getTransaction().rollback(); 
  27.             if (session != null) { 
  28.                 if (session.isOpen()) { 
  29.                     session.close(); 
  30.                 } 
  31.             } 
  32.  
  33.         } 
  34.     } 

基本的操作框架如上面的代码,下面的操作代码添加到//*******//这里//*******//

向数据库中Person表中添加20个Person对象



  1. for(int i=0; i<20; i++){ 
  2.                 Person p = new Person(); 
  3.                 p.setPerName("name_"+i); 
  4.                 p.setCreatTime(new Date()); 
  5.                 System.out.println(p.toString()); 
  6.                 session.save(p);     
  7.             } 

控制太打印输出的SQL语句为:


  1. Person [creatTime=Fri Jul 20 03:14:59 CST 2012, perId=null, perName=name_0] 
  2. Person [creatTime=Fri Jul 20 03:14:59 CST 2012, perId=null, perName=name_1] 
  3. Person [creatTime=Fri Jul 20 03:14:59 CST 2012, perId=null, perName=name_2] 
  4. Person [creatTime=Fri Jul 20 03:14:59 CST 2012, perId=null, perName=name_3] 
  5. .......................................................... 
  6. Person [creatTime=Fri Jul 20 03:14:59 CST 2012, perId=null, perName=name_19] 

上面是创建的每一个对象的信息


  1. Hibernate: insert into ext.dbo.Person (perNamee, creatTime, perId) values (?, ?, ?) 
  2. Hibernate: insert into ext.dbo.Person (perNamee, creatTime, perId) values (?, ?, ?) 
  3. Hibernate: insert into ext.dbo.Person (perNamee, creatTime, perId) values (?, ?, ?) 
  4. Hibernate: insert into ext.dbo.Person (perNamee, creatTime, perId) values (?, ?, ?) 
  5. Hibernate: insert into ext.dbo.Person (perNamee, creatTime, perId) values (?, ?, ?) 
  6. Hibernate: insert into ext.dbo.Person (perNamee, creatTime, perId) values (?, ?, ?) 
  7. Hibernate: insert into ext.dbo.Person (perNamee, creatTime, perId) values (?, ?, ?) 
  8. Hibernate: insert into ext.dbo.Person (perNamee, creatTime, perId) values (?, ?, ?) 
  9. Hibernate: insert into ext.dbo.Person (perNamee, creatTime, perId) values (?, ?, ?) 
  10. Hibernate: insert into ext.dbo.Person (perNamee, creatTime, perId) values (?, ?, ?) 


一共有20条向数据库插入数据的操作。


数据库的显示:

创建实体类使用Hibernate



本文转自 secondriver 51CTO博客,原文链接:http://blog.51cto.com/aiilive/935741,如需转载请自行联系原作者