分享

hibernate创建xml映射和创建数据表

 桑枯海 2012-11-29

1.本文介绍的是:使用MyEclipse自带的xdoclet来在java类中写xdoclet 标记来创建映射.具体如下:

package cn.dao.db;
import java.util.Date;
 /**
  * @author leizhimin
  * @hibernate.mapping default-lazy="false"
  * @hibernate.meta attribute="class-description" value="工作日志"
  * @hibernate.class table="rc_gzrz"
  * package="cn.dao.db"
  */
 public class WorkNote {

 private Long id;//标识

 private Date workDate;//日期

 private String weather;//天气

 private String content; //日志内容(Clob)

 private String state; //日志状态

 private Long orgId; //机构id

 private Long userId;//用户id

 private Date createDate;//创建日期

 private byte[] image; //图片

 public static final String WORKNOTE_BLANK = "00";  //未填写

 public static final String WORKNOTE_FULL = "11";  //已填写
 public WorkNote(){
  
 }
 /**

   * @hibernate.id generator-class="sequence" column="BS"

   * @hibernate.meta attribute="field-description" value="标识"

   * @hibernate.generator-param name="sequence" value="SEQ_GW"
     */


 public Long getId(){
  return id;
 }

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

 /**
  *
  * @hibernate.property column="workDate" not-null="false" type="timestamp"

  * @hibernate.meta attribute="field-description" value="工作日期"
  */

 public Date getWorkDate(){
  return workDate;
 }
 public void setWorkDate(Date workDate) {
  this.workDate = workDate;
 }

 /**

  * @hibernate.property column="weather" not-null="false" length="24"

  * @hibernate.meta attribute="field-description" value="天气"

  */

 public String getWeather() {
  return weather;
 }
 public void setWeather(String weather){
  this.weather = weather;
 }

 /**
  *

   * @hibernate.property column="content" not-null="false" type="text"

   * @hibernate.meta attribute="field-description" value="内容"

   */

 public String getContent() {
  return content;
 }

 public void setContent(String content) {
  this.content = content;
 }

 /**

    * @hibernate.property column="state" not-null="false" length="2"

    * @hibernate.meta attribute="field-description" value="状态"

    */

 public String getState() {
  return state;
 }

 public void setState(String state) {
  this.state = state;
 }

 /**
  *

   * @hibernate.property column="orgId" type="long"

   * @hibernate.meta attribute="field-description" value="机构id"

   */

 public Long getOrgId() {
  return orgId;
 }
 public void setOrgId(Long orgId) {
  this.orgId = orgId;
 }
 /**

   * @hibernate.property column="userId" type="long"

   * @hibernate.meta attribute="field-description" value="用户id"

   */

 public Long getUserId() {
  return userId;
 }
 public void setUserId(Long userId) {
  this.userId = userId;
 }

 /**

   * @hibernate.property column="createDate" not-null="false" type="timestamp"

   * @hibernate.meta attribute="field-description" value="创建日期"

   */

 public Date getCreateDate() {
  return createDate;
 }
 public void setCreateDate(Date createDate) {
  this.createDate = createDate;
 }
 /**

   * @hibernate.property column="image" type="blob" not-null="false"

   * @hibernate.meta attribute="field-description" value="图片"

   */

 public byte[] getImage() {
  return image;
 }

 public void setImage(byte[] image) {
  this.image = image;
 }

}
2.然后: 右键在你的工程的名字点击弹出->properties->点configuration的Add Standard, 然后选择Standard  Hibernate 确定OK;

3.右键在你的工程的名字点击弹出->MyEclipse->run  xdoclet ->没有错误便生成具体类和数据库的.xml映射文件了利用这些映射文件来创建后台数据库的表:

1.先把以上的所有的类的映射xml文件加入到hibernate.cfg.xml:

如: <mapping resource="cn/dao/db/WorkNote.hbm.xml" />

2.在HibernateSessionFactory.java 类中加入一段代码,运行这个类:HibernateSessionFactory 便生成数据库表:

imp
ort org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public static void main(String[] args) {
       
        //读取配置文件
        Configuration cfg = new Configuration().configure();
        
        //创建SchemaExport对象
        SchemaExport export = new SchemaExport(cfg);
        
        //创建数据库表
        export.create(true,true);
    }

或者改为:

 public static void main(String[] args) {
       
        //读取配置文件
        Configuration cfg = (new Configuration()).configure();
        SchemaUpdate update =new SchemaUpdate(cfg);

         //第一个参数:表示sql是否输出到控制台;第二个参数为true 启动hibernate时会自动检查数据库,如果缺少表,则自动建表;
         //如果表里缺少列,则自动添加列。
        update.execute(true, true);
    }

 

===============================

只要在hibernate.cfg.xml添加这句话,就可以自动生成数据表
<property name="hibernate.hbm2ddl.auto">update</property>

update:表示自动根据model对象来更新表结构,启动hibernate时会自动检查数据库,如果缺少表,则自动建表;如果表里缺少列,则自动添加列。

还有其他的参数:
create:启动hibernate时,自动删除原来的表,新建所有的表,所以每次启动后的以前数据都会丢失。

create-drop:启动hibernate时,自动创建表,程序关闭时,自动把相应的表都删除。所以程序结束时,表和数据也不会再存在。

PS:数据库要预先建立好,因为hibernate只会建表,不会建库

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约