温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Hibernate自动生成工具Schema怎么用

发布时间:2021-12-01 09:28:47 来源:亿速云 阅读:121 作者:小新 栏目:编程语言

这篇文章主要介绍Hibernate自动生成工具Schema怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

Hibernate Schema自动生成可以从你的映射文件使用一个Hibernate工具生成DDL。 生成的schema包含有对实体和集合类表的完整性引用约束(主键和外键)。涉及到的标示符生成器所需的表和sequence也会同时生成。

在使用这个工具的时候,你必须 通过hibernate.dialet属性指定一个SQL方言(Dialet),因为DDL是与供应商高度相关的。

首先,要定制你的映射文件,来改善生成的Hibernate schema。对Hibernate schema定制化(Customizing the schema)

很多Hibernate映射元素定义了可选的length、precision 或者 scale属性。你可以通过这个属性设置字段的长度、精度、小数点位数。

<property name="zip" length="5"/> <property name="balance" precision="12" scale="2"/>

有些tag还接受not-null属性(用来在表字段上生成NOT NULL约束)和unique属性(用来在表字段上生成UNIQUE约束)。

<many-to-one name="bar" column="barId" not-null="true"/> <element column="serialNumber" type="long" not-null="true" unique="true"/>

unique-key属性可以对成组的字段指定一个***键约束(unique key constraint)。目前,unique-key属性指定的值在生成DDL时并不会被当作这个约束的名字,它们只是在用来在映射文件内部用作区分的。

<many-to-one name="org" column="orgId" unique-key="OrgEmployeeId"/> <property name="employeeId" unique-key="OrgEmployee"/>

index属性会用对应的字段(一个或多个)生成一个index,它指出了这个index的名字。如果多个字段对应的index名字相同,就会生成包含这些字段的index。

<property name="lastName" index="CustName"/> <property name="firstName" index="CustName"/>

foreign-key属性可以用来覆盖任何生成的外键约束的名字。

<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>

default属性为字段指定一个默认值 (在保存被映射的类的新实例之前,你应该将同样的值赋于对应的属性)。

<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>

sql-type属性允许用户覆盖默认的Hibernate类型到SQL数据类型的映射。

<property name="balance" type="float">     <column name="balance" sql-type="decimal(13,3)"/> property>

check属性允许用户指定一个约束检查。

<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>

以上是“Hibernate自动生成工具Schema怎么用”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI