Detailed Hibernate mapping file

The mapping between Hibernate's persistent classes and relational databases is usually defined using an XML document. This document establishes a one-to-one mapping between persistent classes and database tables through the configuration of a series of XML elements. This means that the mapping document is created according to the definition of the persistent class, not the definition of the table .

1. Root element: <hibernate-mapping>, each hbm.xml file has a unique root element that contains some optional attributes

1) package : Specify a package prefix. If the fully qualified class name is not specified in the mapping document, use this as the package name, such as

<hibernate-mapping package="com.demo.hibernate.beans">

<class name="User" ...>

</hibernate-mapping>

<hibernate-mapping>

<class name="com.demo.hibernate.beans.User" ...>

</hibernate-mapping>

2) schema: the name of the database schema

3) catalog: the name of the database catalog

4) default-cascade : the default cascade style, the default is none

5) default-access: The strategy used by Hibernate to access properties

6) default-lazy : specifies the Java properties and collection classes that do not explicitly indicate the lazy property, what default loading style Hibernate will take, the default is true

7) auto-import : Specifies whether we can use non-fully qualified class names in the query language, the default is true, if there are two persistent classes with the same name in the project, it is best to map the corresponding classes of these two classes The file is configured to false

2. <class> defines a class: a child element of the root element, used to define the mapping relationship between a persistent class and a data table. The following are some optional attributes contained in this element

1) name : Java fully qualified name of the persistent class (or interface), if this property does not exist, Hibernate will assume this is a non-POJO entity mapping

2) table : corresponds to the database table name

3) discriminator-value: The default is the same as the class name, a value used to distinguish different subclasses, used in polymorphic behavior

4) mutable: indicates that the instance of the class is mutable or immutable

5) schema: Override the schema name specified in the root element <hibernate-mapping>

6) catalog: Override the catalog name specified in the root element <hibernate-mapping>

7) proxy: specify an interface to be used as a proxy during lazy loading

8) dynamic-update : The SQL specified for UPDATE will be dynamically generated at runtime and only those fields that have changed will be updated

9) dynamic-insert : The SQL specified for INSERT will be dynamically generated at execution time and only include those non-null fields

10)select-before-update:指定HIbernate除非确定对象真正被修改了(如果该值为true),否则不会执行SQL UPDATE操作。在特定场合(实际上,它只在一个瞬时对象关联到一个新的Session中时执行的update()中生效),这说明Hibernate会在UPDATE之前执行一次额外的SQL SELECT操作,来决定是否应该执行UPDATE

11)polymorphism:多态,界定是隐式还是显式的多态查询

12)where:指定定个附加的SQLWHERE条件,在抓取这个类的对象时会增加这个条件

13)persister:指定一个定制的ClassPersister

14)batch-size:指定一个用于根据标识符(identifier)抓取实例时使用的'batch size'(批次抓取数量)

15)optimistic-lock:乐观锁定,决定乐观锁定的策略

16)lazy:通过设置lazy="false",所有的延迟加载(Lazy fetching)功能将未被激活(disabled)

17)entity-name

18)check:这是一个SQL表达式,用于为自动生成的schema添加多行(multi-row)约束检查

19)rowid

20)subselect

21)abstract:用于在<union-subclass>的继承结构(hierarchies)中标识抽象超类

三、<id>定义主键:Hibernate使用OID(对象标识符)来标识对象的唯一性,OID是关系数据库中主键在Java对象模型中的等价物,在运行时,Hibernate根据OID来维持Java对象和数据库表中记录的对应关系

1)name:持久化类的标识属性的名字

2)type:标识Hibernate类型的名字

3)column:数据库表的主键这段的名字

4)unsaved-value:用来标志该实例是刚刚创建的,尚未保存。可以用来区分对象的状态

5)access:Hibernate用来访问属性值的策略

如果表使用联合主键,那么你可以映射类的多个属性为标识符属性。<composite-id>元素接受<key-property>属性映射和<key-many-to-one>属性映射作为子元素:

以下定义了两个字段作为联合主键:

<composite-id>

<key-property name="username" />

<key-property name="password" />

</composite-id>

 

四、<generator>设置主键生成方式

该元素的作用是指定主键的生成器,通过一个class属性指定生成器对应的类。(通常与<id>元素结合使用)

<id name="id" column="ID" type="integer">

<generator class="native" />--native是Hibernate主键生成器的实现算法之一,由Hibernate根据底层数据库自行判断采用identity、hilo、sequence其中一种作为主键生成方式。

</id>

Hibernate提供的内置生成器:

1)assigned算法

2)hilo算法

3)seqhilo算法

4)increment算法

5)identity算法

6)sequence算法

7)native算法

8)uuid.hex算法

9)uuid.string算法

10)foregin算法

11)select算法

 

五、<property>定义属性

用于持久化类的属性与数据库表字段之间的映射,包含如下属性:

1)name:持久化类的属性名,以小写字母开头

2)column:数据库表的字段名

3)type:Hibernate映射类型的名字

4)update:表明用于UPDATE的SQL语句中是否包含这个被映射的字段,默认为true

5)insert:表明用于INSERT的SQL语句中是否包含这个被映射是否包含这个被映射的字段,默认为true

6)formula:一个SQL表达式,定义了这个计算属性的值

7)access:Hibernate用来访问属性值的策略

8)lazy:指定实例变量第一次被访问时,这个属性是否延迟抓取,默认为false

9)unique:使用DDL为该字段添加唯一的约束,此外,这也可以用做property-ref的目标属性

10)not-null:使用DDL为该字段添加可否为空的约束

11)optimistic-lock:指定这个属性在进行更新时是否需要获得乐观锁定(换句话说,它决定这个属性发生脏数据时版本version的值是否增长)

access属性用来让你控制Hibernate如何在运行时访问属性。默认情况下,Hibernate会使用属性的get/set方法对。如果你指明access="field",则Hibernate会忽略get/set方法对,直接使用反射来访问成员变量。

formula属性是个特别强大的的特征。这些属性应该定义为只读,属性值在装载时计算生成。用一个SQL表达式生成计算的结果,它会在这个实例转载时翻译成一个SQL查询的SELECT子查询语句。如:

<property name="totalPrice" formula="(SELECT SUM(*) FROM user)" />

转载自:https://www.cnblogs.com/ani-deng/p/4772559.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324768777&siteId=291194637