分享java带来的快乐

我喜欢java新东西

Hibernate中使用类映射文件创建数据库表

 

1.加入Hibernate 3支持;
2.定义实体类;
3.创建实体类的映射文件;
4.通过配置文件hibernate.cfg.xml将映射文件加入到Hibernate中;
5.使用SchemaExport类创建数据库。
6.创建序列实现自增字段:
        <id name="id" type="java.lang.Long">
            <column name="ID" precision="22" scale="0" />
            <generator class="sequence">
                <param name="sequence">SEQ_HB_USER_ID</param>
            </generator>
        </id>


hibernate.cfg.xml:
程序代码 程序代码

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

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="connection.username">scott</property>
        <property name="connection.url">
            jdbc:oracle:thin:@127.0.0.1:1521:MGC
        </property>
        <property name="dialect">
            org.hibernate.dialect.Oracle9Dialect
        </property>
        <property name="myeclipse.connection.profile">MGC</property>
        <property name="connection.password">tiger</property>
        <property name="connection.driver_class">
            oracle.jdbc.driver.OracleDriver
        </property>
        <mapping resource="cn/edu/ahau/mgc/hibernate/simple/pojo/User.hbm.xml" />

    </session-factory>

</hibernate-configuration>


User.java:
程序代码 程序代码

package cn.edu.ahau.mgc.hibernate.simple.pojo;

public class User {

    private int id;
    private String userName;
    private String password;

    public int getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}



User.hbm.xml:
程序代码 程序代码

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="cn.edu.ahau.mgc.hibernate.simple.pojo.User" table="HB_USER" schema="SCOTT">
        <id name="id" type="java.lang.Long">
            <column name="ID" precision="22" scale="0" />
            <generator class="sequence">
                <param name="sequence">SEQ_HB_USER_ID</param>
            </generator>
        </id>
        <property name="username" type="java.lang.String">
            <column name="USERNAME" length="10" not-null="true" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="PASSWORD" length="10" not-null="true" />
        </property>
    </class>
</hibernate-mapping>




ExportToDB.java:
程序代码 程序代码

package cn.edu.ahau.mgc.hibernate.simple.export;

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

public class ExportToDB {

    public static void main(String[] args) {
        Configuration cfg = new Configuration().configure();
        SchemaExport export = new SchemaExport(cfg);
        export.create(true, true);
    }

}

posted on 2008-09-24 16:09 强强 阅读(1803) 评论(0)  编辑  收藏 所属分类: Java


只有注册用户登录后才能发表评论。


网站导航: