Property Tag in Hibernate Mapping by R4R Team

Property in Hibernate Mapping: 

<property 

name="propertyName"

column="column_name"  

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" 

/>


Tag

Description

name

In the property tag we use the name for the using the name of the property with an initial lowercase letter

column(optional)

We see the this defaults as property name.

Name of the mapped database table column. This can also be specified by nested <column> elements

type(optional)

This name indicate the hibernate type.

update,insert(optional)

We found this on defaults true.

This specifies that the mapped columns should be included in SQL UPDATE and INSERT statements. We set both to false allows a pure “derived” property whose value is initialized from some other property that maps to the same column or we can say it by a trigger of other application.

formula(optional)

In hibernate an sql expression that defines the value for a computed property. We found the computed property do not have a column mapping of their own.

access(optional)

It set on by defaults to property.

It show the strategy hibernate uses for accessing the property value.

lazy(optional)

It set to defaults false.

It specifies that this property should be fetched lazily when the instances variable is first accessed. It requires build-time bytecode instrumentation.

unique(optional)

It enable the DDL generation of a unique constraint for the column so we allows this to be the target of a property set.

not-null(optional)

It enables the DDL generation of a nullability constraint for the columns.

optimistic-lock(optional)

It found by defaults on true.

It specifies that updates to this property do or do not require acquisition of the optimistic lock. We can say in other word, it determines if a version increment shold occur when this property is dirty.

generated(optional)

It can't set by defaults never.

It specifies that this propety value is actually generated by the database.


we have some property that is given below:

1. Hibernate basic type: integer,string,character,date,timestamp,float,binary,serializable,object,blob.
2. Java class with a default basic type: int, float, char, java.lang.String, java.util.Date, java.util.Integer, java.sql.Clob.
3. Serializable Java class.
4. Some class name of custom type: com.illflow.type.MyCustomType

when we specify a type, In hibernate will use reflection upon the named property and guess the correct Hibernate type. Hibernate will attempt to interpret the name of the return class of the property  with getter using, in order, rules 2, 3, and 4. In certain cases need the type attribute. 

For example, Distinguish between Hibernate.DATE and Hibernate.TIMESTAMP, or to specify a custom type.

The access attribute allows you to control how Hibernate accesses the property at runtime. By default, Hibernate will call the property get/set pair. If specify access="field", Hibernate will bypass the get/set pair and access the field directly using reflection. You can specify your own strategy for property access by naming a class that implements the interface org.hibernate.property.PropertyAccessor.

A powerful feature is derived properties. These properties are by definition read-only. The property value is computed at load time. You declare the computation as an SQL expression. This then translates to a SELECT clause subquery in the SQL query that loads an instance:

<property 
name="totalPrice" 
formula="( SELECT SUM (li.quantity*p.price) FROM LineItem li, Product p WHERE li.productId = p.productId AND li.customerId = customerId AND li.orderNumber = orderNumber )"
/>

Can reference the entity table by not declaring an alias on a particular column. This would be customerId in the given example. You can also use the nested <formula> mapping element if you do not want to use the attribute.return
Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!