HIbernate符合Java习惯的关系数据库持久化之XML配置

[b]Hibernate-mapping[/b]
<hibernate-mapping
schema="schemaName" (1)
catalog="catalogName" (2)
default-cascade="cascade_style" (3)
default-access="field|property|ClassName" (4)
default-lazy="true|false" (5)
auto-import="true|false" (6)
package="package.name" (7)
/>
1、schema (optional): the name of a database schema.
2、catalog (optional): the name of a database catalog.数据库名称
3、default-cascade (optional - defaults to none): a default cascade style.级联
4、default-access (optional - defaults to property): the strategy Hibernate should use for accessing all properties. It can be a custom implementation of PropertyAccessor.
5、default-lazy (optional - defaults to true): the default value for unspecified lazy attributes of class and collection mappings.默认懒加载
6、auto-import (optional - defaults to true): specifies whether we can use unqualified class names of classes in this mapping in the query language.
7、package (optional): specifies a package prefix to use for unqualified class names in the mapping document.
[b]Class[/b]
<class
name="ClassName" (1)
table="tableName" (2)
discriminator-value="discriminator_value" (3)
mutable="true|false" (4)
schema="owner" (5)
catalog="catalog" (6)
proxy="ProxyInterface" (7)
dynamic-update="true|false" (8)
dynamic-insert="true|false" (9)
select-before-update="true|false" (10)
polymorphism="implicit|explicit" (11)
where="arbitrary sql where condition" (12)
persister="PersisterClass" (13)
batch-size="N" (14)
optimistic-lock="none|version|dirty|all" (15)
lazy="true|false" (16)
entity-name="EntityName" (17)
check="arbitrary sql check condition" (18)
rowid="rowid" (19)
subselect="SQL expression" (20)
abstract="true|false" (21)
node="element-name"
/>

1、name (optional): the fully qualified Java class name of the persistent class or interface. If this attribute is missing, it is assumed that the mapping is for a non-POJO entity.
2、table (optional - defaults to the unqualified class name): the name of its database table.
3、[b]discriminator-value [/b](optional - defaults to the class name): a value that distinguishes individual subclasses that is used for polymorphic behavior. Acceptable values include null and not null.
4、mutable (optional - defaults to true): specifies that instances of the class are (not) mutable.Note:易变的,无常的
5、schema (optional): overrides the schema name specified by the root <hibernate-mapping> element.
6、catalog (optional): overrides the catalog name specified by the root <hibernate-mapping> element.
7、proxy (optional): specifies an interface to use for lazy initializing proxies. [b]You can specify the name of the class itself.[/b]
8、dynamic-update (optional - defaults to false): specifies that UPDATE SQL should be generated at runtime and can contain only those columns whose values have changed.
9、dynamic-insert (optional - defaults to false): specifies that INSERT SQL should be generated at runtime and contain only the columns whose values are not null.
10、[b]select-before-update [/b](optional - defaults to false): specifies that Hibernate should never perform an SQL UPDATE unless it is certain that an object is actually modified. Only when a transient object has been associated with a new session using update(), will Hibernate perform an extra SQL SELECT to determine if an UPDATE is actually required.
11、polymorphism (optional - defaults to implicit): determines whether implicit or explicit query polymorphism is used.
12、where (optional): specifies an arbitrary SQL WHERE condition to be used when retrieving objects of this class.
13、persister (optional): specifies a custom ClassPersister.
14、batch-size (optional - defaults to 1): specifies a "batch size" for fetching instances of this class by identifier.
15、optimistic-lock (optional - defaults to version): determines the optimistic locking strategy.
(16)、lazy (optional): lazy fetching can be disabled by setting lazy="false".
(17)、entity-name (optional - defaults to the class name): Hibernate3 allows a class to be mapped multiple times, potentially to different tables. It also allows entity mappings that are represented by Maps or XML at the Java level. In these cases, you should provide an explicit arbitrary name for the entity.
(18)、check (optional): an SQL expression used to generate a multi-row check constraint for automatic schema generation.
(19)、rowid (optional): Hibernate can use ROWIDs on databases. On Oracle, for example, Hibernate can use the rowid extra column for fast updates once this option has been set to rowid. A ROWID is an implementation detail and represents the physical location of a stored tuple.
(20)、subselect (optional): maps an immutable and read-only entity to a database subselect. This is useful if you want to have a view instead of a base table. See below for more information.
(21)、[b]abstract [/b](optional): is used to mark abstract superclasses in <union-subclass> hierarchie
如果你打开了dynamic-update,你可以选择几种乐观锁定的策略:
version: check the version/timestamp columns
all: check all columns
dirty: check the changed columns, allowing some concurrent updates
none: do not use optimistic locking
[b]id[/b]
<id
name="propertyName" (1)
type="typename" (2)
column="column_name" (3)
unsaved-value="null|any|none|undefined|id_value" (4)
access="field|property|ClassName"> (5)
node="element-name|@attribute-name|element/@attribute|."
<generator class="generatorClass"/>
</id>
1、name (optional): the name of the identifier property.
2、type (可选): 一个Hibernate类型的名字。
3、column (optional - defaults to the property name): the name of the primary key column.
4、unsaved-value (optional - defaults to a "sensible" value): an identifier property value that indicates an instance is newly instantiated (unsaved), distinguishing it from detached instances that were saved or loaded in a previous session.
[b]unsaved-value 属性在Hibernate3中几乎不再需要。 [/b]
5、access (optional - defaults to property): the strategy Hibernate should use for accessing the property value.
Generator:
<id name="id" type="long" column="cat_id">
<generator class="org.hibernate.id.TableHiLoGenerator">
<param name="table">uid_table</param>
<param name="column">next_hi_value_column</param>
</generator>
</id>
[b]composite-id[/b]
<composite-id
name="propertyName"
class="ClassName"
mapped="true|false"
access="field|property|ClassName">
node="element-name|."
<key-property name="propertyName" type="typename" column="column_name"/>
<key-many-to-one name="propertyName class="ClassName" column="column_name"/>
......
</composite-id>
第二种方法我们称为mapped(映射式)组合标识符 (mapped composite identifier),<composite-id>元素中列出的标识属性不但在持久化类出现,还形成一个独立的标识符类。
<composite-id class="MedicareId" mapped="true">
<key-property name="medicareNumber"/>
<key-property name="dependent"/>
</composite-id>
下面列出的属性是用来指定一个映射式组合标识符的:
[b] mapped [/b](optional - defaults to false): indicates that a mapped composite identifier is used, and that the contained property mappings refer to both the entity class and the composite identifier class.
[b] class [/b](optional - but required for a mapped composite identifier): the class used as a composite(混合的,组成的)identifier.
[b]Version (optional)[/b]
<version
column="version_column" (1)
name="propertyName" (2)
type="typename" (3)
access="field|property|ClassName" (4)
unsaved-value="null|negative|undefined" (5)
generated="never|always" (6)
insert="true|false" (7)
node="element-name|@attribute-name|element/@attribute|."
/>
1、column (optional - defaults to the property name): the name of the column holding the version number.
2、name: the name of a property of the persistent class.
3、type (optional - defaults to integer): the type of the version number.
4、access (optional - defaults to property): the strategy Hibernate uses to access the property value.
5、unsaved-value (optional - defaults to undefined): a version property value that indicates that an instance is newly instantiated (unsaved), distinguishing it from detached instances that were saved or loaded in a previous session. Undefined specifies that the identifier property value should be used.
6、generated (optional - defaults to never): specifies that this version property value is generated by the database. See the discussion of generated properties for more information.
7、insert (optional - defaults to true): specifies whether the version column should be included in SQL insert statements. It can be set to false if the database column is defined with a default value of 0.
[b]Timestamp (optional)[/b]
<timestamp
column="timestamp_column" (1)
name="propertyName" (2)
access="field|property|ClassName" (3)
unsaved-value="null|undefined" (4)
source="vm|db" (5)
generated="never|always" (6)
node="element-name|@attribute-name|element/@attribute|."
/>
1、column (optional - defaults to the property name): the name of a column holding the timestamp.
2、name: the name of a JavaBeans style property of Java type Date or Timestamp of the persistent class.
3、access (optional - defaults to property): the strategy Hibernate uses for accessing the property value.
4、unsaved-value (optional - defaults to null): a version property value that indicates that an instance is newly instantiated (unsaved), distinguishing it from detached instances that were saved or loaded in a previous session. Undefined specifies that the identifier property value should be used.
5、source (optional - defaults to vm): Where should Hibernate retrieve the timestamp value from? From the database, or from the current JVM? Database-based timestamps incur an overhead because Hibernate must hit the database in order to determine the "next value". It is safer to use in clustered environments. Not all Dialects are known to support the retrieval of the database's current timestamp. Others may also be unsafe for usage in locking due to lack of precision (Oracle 8, for example).
6、generated (optional - defaults to never): specifies that this timestamp property value is actually generated by the database. See the discussion of generated properties for more information.
[b]Property[/b]
<property
name="propertyName" (1)
column="column_name" (2)
type="typename" (3)
update="true|false" (4)
insert="true|false" (4)
formula="arbitrary SQL expression" (5)
access="field|property|ClassName" (6)
lazy="true|false" (7)
unique="true|false" (8)
not-null="true|false" (9)
optimistic-lock="true|false" (10)
generated="never|insert|always" (11)
node="element-name|@attribute-name|element/@attribute|."
index="index_name"
unique_key="unique_key_id"
length="L"
precision="P"
scale="S"
/>
1、[b]name:[/b] 属性的名字,[b]以小写字母开头。[/b]
2、column (optional - defaults to the property name): the name of the mapped database table column. This can also be specified by nested <column> element(s).
3、[b]type (可选):[/b] 一个Hibernate类型的名字。
4、update, insert (optional - defaults to true): specifies that the mapped columns should be included in SQL UPDATE and/or INSERT statements. Setting both to false allows a pure "derived" property whose value is initialized from some other property that maps to the same column(s), or by a trigger or other application.
5、formula (可选): 一个SQL表达式,定义了这个计算 (computed) 属性的值。计算属性没有和它对应的数据库字段。
6、access (optional - defaults to property): the strategy Hibernate uses for accessing the property value.
7、lazy (optional - defaults to false): specifies that this property should be fetched lazily when the instance variable is first accessed. It requires build-time bytecode instrumentation.
8、unique (optional): enables the DDL generation of a unique constraint for the columns. Also, allow this to be the target of a property-ref.
9、not-null (optional): enables the DDL generation of a nullability constraint for the columns.
10、optimistic-lock (optional - defaults to true): specifies that updates to this property do or do not require acquisition of the optimistic lock. In other words, it determines if a version increment should occur when this property is dirty.
11、generated (optional - defaults to never): specifies that this property value is actually generated by the database. See the discussion of generated properties for more information.
typename可以是如下几种:
1.The name of a Hibernate basic type: integer, string, character, date, timestamp, float, binary, serializable, object, blob etc.
2.The name of a Java class with a default basic type: int, float, char, java.lang.String, java.util.Date, java.lang.Integer, java.sql.Clob etc.
3.一个可以序列化的Java类的名字。
4.The class name of a custom type: com.illflow.type.MyCustomType etc.
[b] Many-to-one[/b]
<many-to-one
name="propertyName" (1)
column="column_name" (2)
class="ClassName" (3)
cascade="cascade_style" (4)
fetch="join|select" (5)
update="true|false" (6)
insert="true|false" (6)
property-ref="propertyNameFromAssociatedClass" (7)
access="field|property|ClassName" (8)
unique="true|false" (9)
not-null="true|false" (10)
optimistic-lock="true|false" (11)
lazy="proxy|no-proxy|false" (12)
not-found="ignore|exception" (13)
entity-name="EntityName" (14)
formula="arbitrary SQL expression" (15)
node="element-name|@attribute-name|element/@attribute|."
embed-xml="true|false"
index="index_name"
unique_key="unique_key_id"
foreign-key="foreign_key_name"
/>
1、name: the name of the property.
2、column (optional): the name of the foreign key column. This can also be specified by nested <column> element(s).
3、class (optional - defaults to the property type determined by reflection): the name of the associated class.
4、[b]cascade (optional): specifies which operations should be cascaded from the parent object to the associated object.[/b]
5、fetch (optional - defaults to select): chooses between outer-join fetching or sequential select fetching.
6、update, insert (optional - defaults to true): specifies that the mapped columns should be included in SQL UPDATE and/or INSERT statements. Setting both to false allows a pure "derived" association whose value is initialized from another property that maps to the same column(s), or by a trigger or other application.
7、property-ref (optional): the name of a property of the associated class that is joined to this foreign key. If not specified, the primary key of the associated class is used.
8、access (optional - defaults to property): the strategy Hibernate uses for accessing the property value.
9、unique (optional): enables the DDL generation of a unique constraint for the foreign-key column. By allowing this to be the target of a property-ref, you can make the association multiplicity one-to-one.
10、not-null (optional): enables the DDL generation of a nullability constraint for the foreign key columns.
11、optimistic-lock (optional - defaults to true): specifies that updates to this property do or do not require acquisition of the optimistic lock. In other words, it determines if a version increment should occur when this property is dirty.
12、lazy (optional - defaults to proxy): by default, single point associations are proxied. lazy="no-proxy" specifies that the property should be fetched lazily when the instance variable is first accessed. This requires build-time bytecode instrumentation. lazy="false" specifies that the association will always be eagerly fetched.
13、not-found (optional - defaults to exception): specifies how foreign keys that reference missing rows will be handled. ignore will treat a missing row as a null association.
14、entity-name (optional): the entity name of the associated class.
15、formula (可选): SQL表达式,用于定义computed(计算出的)外键值。
[b]One-to-one[/b]
持久化对象之间一对一的关联关系是通过one-to-one元素定义的。

<one-to-one
name="propertyName" (1)
class="ClassName" (2)
cascade="cascade_style" (3)
constrained="true|false" (4)
fetch="join|select" (5)
property-ref="propertyNameFromAssociatedClass" (6)
access="field|property|ClassName" (7)
formula="any SQL expression" (8)
lazy="proxy|no-proxy|false" (9)
entity-name="EntityName" (10)
node="element-name|@attribute-name|element/@attribute|."
embed-xml="true|false"
foreign-key="foreign_key_name"
/>

1、name: the name of the property.
2、class (optional - defaults to the property type determined by reflection): the name of the associated class.
3、cascade (optional): specifies which operations should be cascaded from the parent object to the associated object.
4、constrained (optional): specifies that a foreign key constraint on the primary key of the mapped table and references the table of the associated class. This option affects the order in which save() and delete() are cascaded, and determines whether the association can be proxied. It is also used by the schema export tool.
5、fetch (optional - defaults to select): chooses between outer-join fetching or sequential select fetching.
6、property-ref (optional): the name of a property of the associated class that is joined to the primary key of this class. If not specified, the primary key of the associated class is used.
7、access (optional - defaults to property): the strategy Hibernate uses for accessing the property value.
8、formula (optional): almost all one-to-one associations map to the primary key of the owning entity. If this is not the case, you can specify another column, columns or expression to join on using an SQL formula. See org.hibernate.test.onetooneformula for an example.
9、lazy (optional - defaults to proxy): by default, single point associations are proxied. lazy="no-proxy" specifies that the property should be fetched lazily when the instance variable is first accessed. It requires build-time bytecode instrumentation. lazy="false" specifies that the association will always be eagerly fetched. Note that if constrained="false", proxying is impossible and Hibernate will eagerly fetch the association.
10、
entity-name (optional): the entity name of the associated class.
There are two varieties of one-to-one associations:
主键关联
惟一外键关联
Primary key associations do not need an extra table column. If two rows are related by the association, then the two table rows share the same primary key value. To relate two objects by a primary key association, ensure that they are assigned the same identifier value.

For a primary key association, add the following mappings to Employee and Person respectively:

<one-to-one name="person" class="Person"/>

<one-to-one name="employee" class="Employee" constrained="true"/>

Ensure that the primary keys of the related rows in the PERSON and EMPLOYEE tables are equal. You use a special Hibernate identifier generation strategy called foreign:

<class name="person" table="PERSON">
<id name="id" column="PERSON_ID">
<generator class="foreign">
<param name="property">employee</param>
</generator>
</id>
...
<one-to-one name="employee"
class="Employee"
constrained="true"/>
</class>

A newly saved instance of Person is assigned the same primary key value as the Employee instance referred with the employee property of that Person.

Alternatively, a foreign key with a unique constraint, from Employee to Person, can be expressed as:

<many-to-one name="person" class="Person" column="PERSON_ID" unique="true"/>

This association can be made bidirectional by adding the following to the Person mapping:

<one-to-one name="employee" class="Employee" property-ref="person"/>
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页