服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

值得关注的持久化技术: hibernate


  1.简述:
  学习一项新的技术要花时间的,它会不会把时间在使用它之后补回来是应该关注的问题:我们可以用关键字:"jdo ejb cmp hibernate"在google上找一找对他们的评论文章,其中只要使用过hibernate的人都对它很推崇(我也是).
  
  我们的对象模型可以被hibernate很好的利用,下图中的persistent object是简单的业务实体对象(要被持久化的对象)。通过hibernate被透明的持久化到数据库中。下面的例子会说明一切。
  
. 值得关注的持久化技术: hibernate(图一)

  2.一个简单的例子
  我们开发一个person类:
  
. 值得关注的持久化技术: hibernate(图二)

  可以想象数据表是下面的样子:
  
  表person
  
. 值得关注的持久化技术: hibernate(图三)

  要通过hibernate实现它要经过以下步骤:
  
  主要是先根据对象模型编写简单的业务实体类(简单的bean规范类,即有get,set方法的类,可以用jac等工具生成),再用xml映射文件描述其映射数据库的方式(很easy),最后就可以通过很少的hibernate api写测试类对其进行持久化操作。
  
  2.1 写person类(persistent object),它和普通的类没甚末不同,但注意它应该复合bean的规范,为每个属性提供get,set方法:
  
  h.person
  //
  // warning: this file has been automatically generated by jac
  // do not modify unless you know what you are doing
  //
  
  package h;
  
  public class person {
  
  string name;
  
  /**
  sets the value of field name.
  @param name value of field name
  */
  
  public void setname(string value) {
    this.name=value;
  }
  
  /**
  gets the value of field name.
  @return value of field name
  */
  
  public string getname() {
    return name;
  }
  
  string address;
  
  /**
  sets the value of field address.
  @param address value of field address
  */
  
  public void setaddress(string value) {
    this.address=value;
  }
  
  /**
  gets the value of field address.
  @return value of field address
  */
  
  public string getaddress() {
    return address;
  }
  
  string id;
  
  /**
  sets the value of field id.
  @param id value of field id
  */
  
  public void setid(string value) {
    this.id=value;
  }
  
  /**
  gets the value of field id.
  @return value of field id
  */
  
  public string getid() {
    return id;
  }
  }
  2.2 填写对象-关系映射xml文件,不用担心它的复杂程度,我们可以抄嘛:和所有o-r 映射工具一样都要写这种映射文件。
  
  person.hbm.xml 建议命名为:"类名"+"hbm.xml" 并且放置在person类相同包目录下
  <?xml version="1.0" encoding="gb2312"?>
  <!doctype hibernate-mapping system "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
  <hibernate-mapping>
  
   <class name="h.person">
     <!--hibernate为我们生成主键id-->
   <id name = "id" unsaved-value = "null">
   <generator class="uuid.hex"/>
   </id>
  
     <!--默认把类的变量映射为相同名字的表列,当然我们可以修改其映射方式-->
   <property name="name"/>
   <property name="address"/>
   </class>
  </hibernate-mapping>
  
  2.3 testperson 我们用hibernate api来进行持久化person
  
  h.testperson 
  package h;
  
  import net.sf.hibernate.session;
  import net.sf.hibernate.transaction;
  import net.sf.hibernate.sessionfactory;
  import net.sf.hibernate.cfg.configuration;
  import net.sf.hibernate.tool.hbm2ddl.schemaexport;
  
  class testperson{
  
   private static sessionfactory sessions;
  
  public static void main(string[] args) throws exception{    
   //配置环境,分析xml映射文件
   configuration conf= new configuration()
   .addclass(person.class);
    
    //生成并输出sql到文件(当前目录)和数据库
    schemaexport dbexport=new schemaexport(conf);
    dbexport.setoutputfile("build//sql.txt");
    dbexport.create(true, true);
  
   sessions = conf.buildsessionfactory();
    //以上都是些固定格式的环境配置
    
    //start......
    session s = sessions.opensession();
   transaction t = s.begintransaction();
     
    //1.用普通使用方式建立对象,填充数据
     person p1=new person();
     p1.setname("陈鹏");
     p1.setaddress("西安东郊黄陵");
     
     //2.持久化
     s.save(p1);
   //此时p1已经可以在数据库中找到
  
     t.commit();
     s.close();
  }
  }
  
  2.4 为了运行当然要配置数据库:我以mysql数据库为例子:(只用劳动1次即可)
  
  hibernate.properties 在hibernate源程序的根目录可以找到此文件模板,copy到我们的类的根目录。即:“../h”
  ## mysql
  ## 前两行人家都给你填好了,只用填数据库连接和username,password
  hibernate.dialect net.sf.hibernate.dialect.mysqldialect
  hibernate.connection.driver_class org.gjt.mm.mysql.driver
  hibernate.connection.url jdbc:mysql://localhost/test?useunicode=true&characterencoding=gbk
  hibernate.connection.username root
  hibernate.connection.password
  
  2.5 运行testperson类,在mysql中观察create好的表person和其中的数据
  
  ok!整个过程主要的是前3步:写bean类(persistent object),写o-r映射文件,写存取测试类。
  
  3. 复杂关系例子
  我们换一个稍微复杂的例子来看看:
  
. 值得关注的持久化技术: hibernate(图四)

  可以想象数据表是下面的样子:
  
  表person
  
. 值得关注的持久化技术: hibernate(图五)

  表company
  
. 值得关注的持久化技术: hibernate(图六)

  3.1 person(已有),company类:persistent object
  
  h.company
  package h;
  public class company {
  string id;
  public void setid(string value) {
    this.id=value;
  }
  public string getid() {
    return id;
  }
  string name;
  public void setname(string value) {
    this.name=value;
  }
  public string getname() {
    return name;
  }
  java.util.list employees=new java.util.vector();
  public void setemployees(java.util.list value) {
    this.employees=value;
  }
  public java.util.list getemployees() {
    return employees;
  }
  public void addemployee(h.person value) {
    employees.add(value);
  }
  public void removeemployee(h.person value) {
    employees.remove(value);
  }
  public void clearemployees() {
    employees.clear();
  }
  }
  
  3.2 填写对象-关系映射xml文件,person.hbm.xml照旧
  
  company.hbm.xml 也放到h.company类相同目录下
  <?xml version="1.0" encoding="gb2312"?>
  <!doctype hibernate-mapping system "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
  <hibernate-mapping>
   <class name="h.company">
     <!--hibernate为我们生成主键id-->
   <id name = "id" unsaved-value = "null">
   <generator class="uuid.hex"/>
   </id>
  
    <property name="name"/>
     <!--1:n关系的映射-->
     <list name="employees" cascade="all">
      <key column="company_id"/>
      <index column="posn"/>

扫描关注微信公众号