Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lect17-hibernate-orm / more / hibernate_reference.pdf
Скачиваний:
54
Добавлен:
18.03.2015
Размер:
2.2 Mб
Скачать

Property

insertable (optional): whether or not the column will be part of the insert statement (default

true)

updatable (optional): whether or not the column will be part of the update statement (default

true)

columnDefinition (optional): override the sql DDL fragment for this particular column (non portable)

table (optional): define the targeted table (default primary table)

length (optional): column length (default 255)

precision (optional): column decimal precision (default 0)

scale (optional): column decimal scale if useful (default 0)

5.1.4.1.5. Formula

Sometimes, you want the Database to do some computation for you rather than in the JVM, you might also create some kind of virtual column. You can use a SQL fragment (aka formula) instead of mapping a property into a column. This kind of property is read only (its value is calculated by your formula fragment).

@Formula("obj_length * obj_height * obj_width")

public long getObjectVolume()

The SQL fragment can be as complex as you want and even include subselects.

5.1.4.1.6. Non-annotated property defaults

If a property is not annotated, the following rules apply:

If the property is of a single type, it is mapped as @Basic

Otherwise, if the type of the property is annotated as @Embeddable, it is mapped as @Embedded

Otherwise, if the type of the property is Serializable, it is mapped as @Basic in a column holding the object in its serialized version

Otherwise, if the type of the property is java.sql.Clob or java.sql.Blob, it is mapped as

@Lob with the appropriate LobType

5.1.4.2. Property mapping with hbm.xml

The <property> element declares a persistent JavaBean style property of the class.

<property

name="propertyName"

column="column_name"

101

Chapter 5. Basic O/R Mapping

type="typename"

update="true|false"

insert="true|false"

formula="arbitrary SQL expression"

access="field|property|ClassName"

lazy="true|false"

unique="true|false"

not-null="true|false"

optimistic-lock="true|false"

generated="never|insert|always" node="element-name|@attribute-name|element/@attribute|." index="index_name"

unique_key="unique_key_id" length="L"

precision="P"

scale="S"

/>

name: the name of the property, with an initial lowercase letter.

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

type (optional): a name that indicates the Hibernate type.

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.

formula (optional): an SQL expression that defines the value for a computed property. Computed properties do not have a column mapping of their own.

access (optional - defaults to property): the strategy Hibernate uses for accessing the property value.

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. unique (optional): enables the DDL generation of a unique constraint for the columns. Also, allow this to be the target of a property-ref.

not-null (optional): enables the DDL generation of a nullability constraint for the columns. 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.

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 could be:

1.The name of a Hibernate basic type: integer, string, character, date, timestamp, float, binary, serializable, object, blob etc.

102

Соседние файлы в папке more