Hibernate 3.6.10.Final ²Î¿¼ÊÖ²á ÖÐÎÄ°æ

Hibernate

Hibernate.org Community Documentation

  • Mapping Editor: Hibernate XML 映射文件的编辑器,支持自动完成和语法高亮。它也支持对类名和属性/字段名的语义自动完成,比通常的 XML 编辑器方便得多。

  • Console: Console 是 Eclipse 的一个新视图。除了对你的 console 配置的树状概览,你还可以获得对你持久化类及其关联的交互式视图。Console 允许你对数据库执行 HQL 查询,并直接在 Eclipse 中浏览结果。

  • Development Wizards: 在 Hibernate Eclipse tools 中还提供了几个向导;你可以用向导快速生成 Hibernate 配置文件(cfg.xml),你甚至还可以同现存的数据库 schema 中反向工程出 POJO 源代码与 Hibernate 映射文件。反向工程支持可定制的模版。

<property name="zip" length="5"/>
<property name="balance" precision="12" scale="2"/>
<many-to-one name="bar" column="barId" not-null="true"/>
<element column="serialNumber" type="long" not-null="true" unique="true"/>
<many-to-one name="org" column="orgId" unique-key="OrgEmployeeId"/>
<property name="employeeId" unique-key="OrgEmployee"/>
<property name="lastName" index="CustName"/>
<property name="firstName" index="CustName"/>
<many-to-one name="bar" column="barId" foreign-key="FKFooBar"/>
<property name="name" type="my.customtypes.Name"/>
    <column name="last" not-null="true" index="bar_idx" length="30"/>
    <column name="first" not-null="true" index="bar_idx" length="20"/>
    <column name="initial"/>
</property
>
<property name="credits" type="integer" insert="false">
    <column name="credits" default="10"/>
</property
>
<version name="version" type="integer" insert="false">
    <column name="version" default="0"/>
</property
>
<property name="balance" type="float">
    <column name="balance" sql-type="decimal(13,3)"/>
</property
>
<property name="foo" type="integer">
    <column name="foo" check="foo 
> 10"/>
</property
>
<class name="Foo" table="foos" check="bar < 100.0">
    ...
    <property name="bar" type="float"/>
</class
>
属性(Attribute) 值(Values) 解释(Interpretation)
length 数字 字段长度
precision 数字 精度(decimal precision)
scale 数字 小数点位数(decimal scale)
not-null true|false 指明字段是否应该是非空的
unique true|false 指明是否该字段具有惟一约束
index index_name 指明一个(多字段)的索引(index)的名字
unique-key unique_key_name 指明多字段惟一约束的名字(参见上面的说明)
foreign-key foreign_key_name 指明一个外键的名字,它是为关联生成的,或者是为 <one-to-one>, <many-to-one>, <key>, or <many-to-many> 映射元素。注意 inverse="true" 会被 SchemaExport 忽略。
sql-type SQL column type 覆盖默认的字段类型(只能用于 <column> 属性)
default SQL 表达式 为字段指定默认值
check SQL 表达式 对字段或表加入 SQL 约束检查

<class name="Customer" table="CurCust">
    <comment
>Current customers only</comment>
    ...
</class
>
<property name="balance">
    <column name="bal">
        <comment
>Balance in USD</comment>
    </column>
</property
>
选项 描述
--quiet 不要把脚本输出到 stdout
--drop 只进行 drop tables 的步骤
--create 只创建表
--text 不执行在数据库中运行的步骤
--output=my_schema.ddl 把输出的 ddl 脚本输出到一个文件
--naming=eg.MyNamingStrategy 选择 NamingStrategy
--config=hibernate.cfg.xml 从 XML 文件读入 Hibernate 配置
--properties=hibernate.properties 从文件读入数据库属性
--format 把脚本中的 SQL 语句对齐和美化
--delimiter=; 为脚本设置行结束符

Configuration cfg = ....;

new SchemaExport(cfg).create(false, true);
  • 通过 -D<property> 系统参数

  • hibernate.properties 文件中

  • 位于一个其它名字的 properties 文件中,然后用 --properties 参数指定

属性名 描述
hibernate.connection.driver_class jdbc driver class
hibernate.connection.url jdbc url
hibernate.connection.username database user
hibernate.connection.password user password
hibernate.dialect 方言(dialect)

<target name="schemaexport">
    <taskdef name="schemaexport"
        classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
        classpathref="class.path"/>
    
    <schemaexport
        properties="hibernate.properties"
        quiet="no"
        text="no"
        drop="no"
        delimiter=";"
        output="schema-export.sql">
        <fileset dir="src">
            <include name="**/*.hbm.xml"/>
        </fileset>
    </schemaexport>
</target
>
选项 描述
--quiet 不要把脚本输出到 stdout
--text 不把脚本输出到数据库
--naming=eg.MyNamingStrategy 选择 NamingStrategy
--properties=hibernate.properties 从文件读入数据库属性
--config=hibernate.cfg.xml 指定一个 .cfg.xml 文件

Configuration cfg = ....;

new SchemaUpdate(cfg).execute(false);
<target name="schemaupdate">
    <taskdef name="schemaupdate"
        classname="org.hibernate.tool.hbm2ddl.SchemaUpdateTask"
        classpathref="class.path"/>
    
    <schemaupdate
        properties="hibernate.properties"
        quiet="no">
        <fileset dir="src">
            <include name="**/*.hbm.xml"/>
        </fileset>
    </schemaupdate>
</target
>
选项 描述
--naming=eg.MyNamingStrategy 选择 NamingStrategy
--properties=hibernate.properties 从文件读入数据库属性
--config=hibernate.cfg.xml 指定一个 .cfg.xml 文件

Configuration cfg = ....;

new SchemaValidator(cfg).validate();
<target name="schemavalidate">
    <taskdef name="schemavalidator"
        classname="org.hibernate.tool.hbm2ddl.SchemaValidatorTask"
        classpathref="class.path"/>
    
    <schemavalidator
        properties="hibernate.properties">
        <fileset dir="src">
            <include name="**/*.hbm.xml"/>
        </fileset>
    </schemavalidator>
</target
>