45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 电脑教程 > 阅读资讯:Hibernate配置文件标签大全

Hibernate配置文件标签大全

2016-08-29 08:35:01 来源:www.45fan.com 【

Hibernate配置文件标签大全

1.<hibernate-mapping> Element
? package: Fully qualifies unqualified classes in the mapping document. As you’ll see later, the
<class> element includes a name attribute that relates to the name of the Java class being
mapped. You can choose to use the package attribute and fully qualify the Java class. For
example:
<hibernate-mapping package="com.company">
<class name="Account"/>
</hibernate-mapping>
In this example, we're mapping the class com.company.Account. If we didn't add the
package attribute to the element, the system would expect to map a class called Account.
补充:这里class的路径都是以hibernate.cfg.xml/hibernate.propertites的目录为根目录进行定位。
? schema: Like the package attribute, fully qualifies the table used in this particular mapping. If
you add the schema attribute, the supplied namespace is appended to the table. For example:
<hibernate-mapping schema="products">
<class table="CD"> </class>
</hibernate-mapping>
In this example, the fully qualified database/table is products.CD. The schema attribute is
optional.
2.<class> Element
? name: Specifies the name of the Java class this mapping document is designed to represent.
Hibernate expects a fully qualified class name in this attribute if you don’t include the package
attribute in the <hibernate-mapping> element.
? table: Specifies the name of the table to be used when persisting the Java object for this mapping.
? schema: Overrides the value for the schema attribute specified in the <hibernate-mapping>
root element (if such a value has been set).
3.<id> Element
? name: Specifies the name of the ID. This is an optional attribute; it’s included in many mappings
with a value of "id".
? type: Specifies the Hibernate type for the ID. If a type isn’t provided, Hibernate uses reflection
to try to determine the type. For a list of the Hibernate types, see the later description of the
<property> element.
? column: Specifies the name of the database column. This is an optional attribute; if it isn’t specified,
the database column name is the value in the name attribute. You need to include either the
name or the column attribute in the <id> element.
? unsaved-value: Determines whether a newly instantiated object needs to be persisted . This is an optional attribute; the default is null.
? access: Specifies the way Hibernate accesses an object's attribute using traditional JavaBean
style setter/getter methods. The values available for the access attribute are field, property,
or a class name. If you want Hibernate to use reflection and access the Java object's attribute
directly, use the value "field". To build your own class for accessing the attribute of an object,
use as a value a class that implements net.sf.hibernate.property.PropertyAccessor.
? <generator> subelement: Defined in the following section, “<generator> Element.”
4.<generator> Element
下面是各个generator的介绍:

 

Generator Description
increment It generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. It should not the used in the clustered environment.
identity It supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int.
sequence The sequence generator uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or a generator in Interbase. The returned identifier is of type long, short or int
hilo The hilo generator uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a table and column (by default hibernate_unique_key and next_hi respectively) as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database. Do not use this generator with connections enlisted with JTA or with a user-supplied connection.
seqhilo The seqhilo generator uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a named database sequence.
uuid The uuid generator uses a 128-bit UUID algorithm to generate identifiers of type string, unique within a network (the IP address is used). The UUID is encoded as a string of hexadecimal digits of length 32.
guid It uses a database-generated GUID string on MS SQL Server and MySQL.
native It picks identity, sequence or hilo depending upon the capabilities of the underlying database.
assigned lets the application to assign an identifier to the object before save() is called. This is the default strategy if no <generator> element is specified.
select retrieves a primary key assigned by a database trigger by selecting the row by some unique key and retrieving the primary key value.
foreign uses the identifier of another associated object. Usually used in conjunction with a <one-to-one> primary key association.


Increment
The increment generator is probably the most familiar creator of IDs. Each time the generator needs to
generate an ID, it performs a select on the current database, determines the current largest ID value, and
increment to the next value. Note that if you’re in a multithreaded environment this generator isn’t safe,
because two or more threads could obtain the same ID value and cause an exception on the database
server.
This generator supports all databases. It has no parameters. The possible column types are short, int, and
long. Here’s some example output:
+--------+------------+----------------------+---------------------+------+
| ID | title | artist | purchasedate | cost |
+--------+------------+----------------------+---------------------+------+
| 1 | Rush | Grace Under Pressure | 2004-04-03 00:00:00 | 9.99 |
| 2 | Nickelback | The Long Road | 2004-04-03 00:00:00 | 11.5 |
+--------+------------+----------------------+---------------------+------+


Identity
If the database has an identity column associated with it, Hibernate can take advantage of the column to
indicate when an object hasn’t been added to the database. Supported databases include DB2, MySQL,
Microsoft SQL Server, Sybase, and HypersonicSQL. Be sure you use this generator with the correct
database. It has no parameters. The possible column types are short, int, and long.

Hilo

The hilo generator generates unique IDs for a database table. The IDs won’t necessarily be sequential.
This generator must have access to a secondary table that Hibernate uses to determine a seed value for
the generator. The default table is hibernate-unique-key, and the required column is next-value.
You need to insert one row in the table. Here's a possible table-creation query:
create table hibernate_unique_key (next_value int
);
insert into hibernate-unique-key values(100);
The hilo generator must be used with a Hibernate-generated connection. It shouldn't be used with JTA
connections, according to the Hibernate reference manual.
You can provide three parameters to this generator to change the table name, column name, and maximum
low value. For example:
<generator class="hilo">
<param name="table">hilo</param>
<param name="column">next</param>
<param name="max to">500</param>
</generator>
The possible column types are short, int, and long. Here's some example output:
mysql> select * from cd;
+--------+------------+----------------------+---------------------+------+
| ID | title | artist | purchasedate | cost |
+--------+------------+----------------------+---------------------+------+
| 131073 | Rush | Grace Under Pressure | 2004-04-03 00:00:00 | 9.99 |
| 163841 | Nickelback | The Long Road | 2004-04-03 00:00:00 | 11.5 |
+--------+------------+----------------------+---------------------+------+

seqhilo

With the seqhilo generator, Hibernate combines the sequence and hilo generators. Supported databases
include DB2, PostgreSQL, Oracle, SAP DB, McKoi, and InterBase. Be sure you use this generator with
the correct database.
You can provide a sequence name with a value of hi_value or low_value depending on the sequence
start. You can also provide a max_lo name with the starting value of the sequence. The possible column
types are short, int, and long.

Uuid.hex

This generator creates a unique strings based on appending the following values: the machine's IP
address, the startup time of the current JVM, the current time, and the counter value. It supports all databases and has no parameters. The possible column types are string, varchar, and text. Here's some
example output:
mysql> select * from cd;
+-------------------+-------+---------------------+---------------------+----+
| ID | title | artist | purchasedate |cost|
+-------------------+-------+---------------------+---------------------+----+
| 40288195fbd50bb...| Rush | Grace Under Pressure| 2004-04-10 00:00:00 |9.99|
+-------------------+-------+---------------------+---------------------+----+
1 row in set (0.00 sec)

Uuid.string

This generator is like Uuid.hex, but the result is a string 16 characters long. It supports all databases except
PostgreSQL, and it has no parameters. Possible column types are string, varchar, and text. In the following
example output, the Marcus Roberts CD is using uuid.string:
mysql> select * from cd;
+--------------+--------------+---------------------+-------------------+-----+
| ID | title | artist | purchasedate |cost |
+--------------+--------------+---------------------+-------------------+-----+
| 40288195f... | Rush |Grace Under Pressure|2004-04-10 00:00:00 |9.99 |
| _¿_§{U?_?... |Marcus Roberts|the truth is spoken |2004-04-10 00:00:00 |13.88|
+--------------+--------------+---------------------+-------------------+-----+
2 rows in set (0.00 sec)

Native

The native generator picks identity, sequence, or hilo, depending on the database.

Assigned

If you need to assign the identifier yourself in your application, then use the assigned generator. You set
the identifier using the set<identifier> method of the Java class. The application assumes the
responsibility of making sure the id value is unique; otherwise, you'll probably get a database insert
exception.
This generator supports all databases. Some level of application validation may need to occur. It has no
parameters. The column types are application dependent.

Foreign

When a class is part of a one-to-one relationship, it can be helpful to have a common ID between the objects.
By specifying the foreign generator, you make the class use the ID of the other class. This generator supports
all databases and has no parameters.

5.<version> Element

If data in your object needs to be versioned as opposed to having a unique ID, you can use the <version>
or <timestamp> element to keep the data up to date. There is no generator for
the version or timestamp. The application must handle populating the Java class attribute with the
correct value.

? column: Specifies the name of the column to use for the identifier. This attribute is optional; the
default is the value of the name attribute.
? name: Specifies the name of the Java class's version attribute.
? type: Specifies the type of the version attribute. This attribute is optional; the default is int.
? access: See the description in our discussion of the <id> element.
? unsaved-value: Indicates whether the object is newly instantiated. Hibernate uses the value to
determine whether the object needs to be persisted. This is an optional attribute; the default is
undefined.

6.<timestamp> Element

The <timestamp> element is used in much the same manner as the <version> element. It has five
attributes:

? column: Specifies the name of the column to use for the identifier. This attribute is optional; the
default is the value of the name attribute.
? name: Specifies the name of the Java class’s timestamp attribute.
? type: Specifies the type of the timestamp attribute. This attribute is optional; the default is
int.
? access: See the description in our discussion of the <id> element.
? unsaved-value: Indicates whether the object is newly instantiated. Hibernate uses the value to
determine whether the object needs to be persisted. This is an optional attribute; the default is
undefined.

7.<discriminator> Element

If you're mapping a hierarchy of objects with Hibernate, you have the option of using a mapping of a
table-per-class-hierarchy scheme. For the single table to work properly, a column is used to distinguish one class from another in the hierarchy. The <discriminator> element identifies the column in the table.The element has three attributes:
? column: Specifies the name of the column in the table to use as the discriminator column. This
attribute is optional; the default is class.
? type: Specifies the type of the discriminator column. The default is string if the attribute isn't
included. The valid types are string, character, integer, byte, short, Boolean, yes no, and true
false. Some of these types will only handle two different classes.
? force: Determines whether Hibernate uses discriminator values when retrieving all instances of
a root class. This attribute is optional; the default is false.


Notice that the element doesn't specify the value to place in the column. The value is determined with
the discriminator-value attribute.


8.<subclass> Element

When an inheritance relationship is defined for your Java classes, the hierarchy needs to be defined.
Since Hibernate recommends the table-per-class-hierarchy mapping, you should define all subclasses
with the <subclass> element. Subclass definitions include attributes as well as the properties that have
been added to this specific subclass.This element has the following attributes:

? name: Specifies the name of the Java class this mapping document is designed to represent.
Hibernate expects a fully qualified class name in this attribute if you don't have the package
attribute in the <hibernate-mapping> element.
? discriminator-value: Specifies the value used to distinguish between different classes in an
inheritance hierarchy. The default is the class name.
? proxy: Tells Hibernate whether to use lazy initialization for objects of this mapping type. Lazy
initialization allows Hibernate to basically stub out the data in an object. If you're using lazy initialization,
you create objects and load them just as in the example application in Chapter 3, but
Hibernate won't populate or make a call to the database for data until the object is used. The
proxy attribute requires you to provide a class that is used for the lazy initialization; this should
normally be the class name used in the name attribute.
? dynamic-update: If true, then when Hibernate updates a row in the database for a class
attribute that has changed, it generates an UPDATE SQL statement at runtime during the
update() method call on the session and only include the columns of the table that have been
updated. This is an optional attribute; the default is false.
? dynamic-insert: The same as dynamic-update in principle; but Hibernate dynamically creates
an INSERT SQL statement with only those columns that aren't null values. This is an optional
attribute; the default is false.

Once the attributes have been defined for the subclass, you need to define all the new attributes that
should be persisted. Use the <property> element to define each attribute.

9.<joined-subclass> Element

If you don't want to use the recommended inheritance mapping, you can choose the table-persubclass
mapping. Each subclass must have its own <joined-subclass> element. You include all
the usual elements like <property>, <key>, and so on for this subclass.

This element has the following attributes:
? name: Specifies the name of the Java class this mapping document is designed to represent.
Hibernate expects a fully qualified class name in this attribute if you don't have the package
attribute in the <hibernate-mapping> element.
? proxy: Tells Hibernate whether to use lazy initialization for objects of this mapping type. Lazy
initialization allows Hibernate to basically stub out the data in an object. If you’re using lazy initialization,
you create your objects and load them, but Hibernate won't populate or make a call to the database for data until the object is used. The proxy attribute requires you to provide a class that is used for the lazy initialization;
this should normally be the class name used in the name attribute.
? dynamic-update: If true, then when Hibernate updates a row in the database for a class
attribute that has changed, it generates an UPDATE SQL statement at runtime during the
update() method call on the session and only include the columns of the table that have been
updated. This is an optional attribute; the default is false.
? dynamic-insert: The same as dynamic-update in principle; but Hibernate dynamically creates
an INSERT SQL statement with only those columns that aren’t null values. This is an optional
attribute; the default is false.

10.<key> Element

Since a collection is owned by a class, you use the <key> element to indicate the property of the class.
The format is:

<key column="column"/>

This element has a single attribute: column, which specifies the foreign key column.

 

本文地址:http://www.45fan.com/dnjc/69221.html
Tags: 标签 配置文件 hibernate
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部