Types of Hibernate in Hibernate by R4R Team

There are some basic type of hibernate are available those are given below:


1. Custom value types: Custom value types are relatively easy for the developers to create their own value types. We take a small example to understand this, if we want to persist properties of the java.lang.BigInteger to VARCHAR column. Then in the hibernate does not provide the built-in-type for this. We know that the custom types are no limited to mapping a property or we can say that it is not having any boundation for the collection of the elements to a single table columns. Example for this if we have a java property getName()/setName() of the type java.lang.String which persisted to the columns FIRST_NAME,INITAL,SURNAME.


In hibernate if we required to implement the custom type we can implement either org.hibernate.UserType or org.hibernate.CompositeUserType and declare properties using the fully qualified class name of the type. To seeing this we required the org.hibernate.DoubleStringType how much thing are possible.


<property name="twoStrings" type="org.hibernate.test.DoubleStringType"> <column name="first_string"/> <column name="second_string"/> </property>


We have to remember the use of <column> tags to map a property to multiple columns. In the CompositeUserType, EnhancedUserType, UserCollectionType and UserVersionType interfaces provide support for if we want to do the more specilized uses. For more specilization we can supply parameters to UserType in the mapping file for doing this we need UserType must implement with org.hibernate.usertype.ParameterizedType interface. For supplying the parameters for the custom type we can use the type <type> element in the mapping files as given below:


<property name="priority"> <type name="com.mycompany.usertypes.DefaultValueIntegerType"> <param name="default">0</param> </type> </property>


<property name="priority" type="default_zero"/>


We know that in the hibernate everything is possible to override the parameters supplied in a typedef on a case-by-case basic by using type of parameters on the property mapping. Even more Hibernate rich range of built-in-type and support for components it means we realy need to use a custom type. It is considered good practice to use custom type for non-entity class that occur frequenty in the application. We can take a example a MonetaryAmount class is a good candidate for a CompositeUserType , even though it could be mapped as a componant. one is the resion behind for this abstraction that is mapping with custom type, if we do then we can manage mapping documents and that will be protected against change to the way monetary values are represnted.


2. Basic Value Types: The baisic value type are categorized in the follwing part as given below:


Basic Value Types

Description

Integer,long,short,float,double,character,byte,boolean,yes_no,true_false

It works for the mapping with java premitives or wrapper class to appropriate (vendor-specifice) SQL column types. Boolean yes_no and true_false are the alternative encoding for a Java boolean or java.lang.Boolean.

String

It is a type of mapping from java.lang.String to VARCHAR (or in Oracle VARCHAR2)

Date,time,timestamp

It is the type from java.lang.util.Date and its subclasses to SQL types DATE,TIME,TIMESTAMP (or equivalant)

Calandar,calandar_date

It is the type of java.util.Calandar to SQL types TIMESTAMP and DATE (or equivalent)

big_decimal,big_integer

They do mapping form java.math.BigDecimal and java.math.BigInteger to NUMERIC (or in Oracle NUMBER)

Locale,timezone,currency

It comes form java.util.Locale, and java.util.TimeZone and java.util.Currency to VARCHAR (or in Oracle VARCHAR2). Here the instance of Locale and Currency are mapped to their ISO codes. Instances of TimeZone are mapped to their ID.

Class

This is also a type of mapping which comes form java.lang.Class to VARCHAR (or in Oracle VARCHAR2). A class is mapped to its fully qualified name.

Binary

It maps byte arrays to an appropriate SQL binary type.

Text

It maps fom java string to a SQL CLOB or TEXT type.

Serializable

It maps serializable java class to an appropriate SQL binary type. So then we can also indicate the hibernate type serializable with the name of a serializable java class or interface that does not default to a basic type.

Clob,blob

These type of mapping for JDBC class java.sql.Clob and java.sql.Blob. These type can be inconveniet for some applications that since the blob or clob object cannot be reused outside of a transaction. Driver support is patchy and inconsistent.

imm_date,imm-time,imm-timestamp,imm_calandar,imm_calandar_date,imm_serializable,imm_binary

These type of mapping work for what are considered mutable java types. This is where hibenate makes certain optimization appropriate only for immutable java types, and the application treates the object as immutable. We can take a example if we should not call Date.setTime() for an instance mapped as imm_timestamp. So if we need to change the property and have change made persitent, the application must assign a new nonidentical object to the property. These are unique identifiers of the entites and collection can be of any basic type except binary, blob and clob. Composite identifier are also allowed. So for this we need to follow the given information : the basic value types have corresponding type constants defined on org.hibernate.Hibernate as Hibernate.STRING represent the string type.


3. Entities and values: For making the relation to the persistence service, java language-level objects are classified into two group which is given below:


An entity exists independently of any other objects holding references to the entity. Contrast this with the usual Java model, where an unreferenced object is garbage collected. Entities must be explicitly saved and deleted. Saves and deletions, however, can be cascaded from a parent entity to its children. This is different from the ODMG model of object persistence by reachability and corresponds more closely to how application objects are usually used in large systems. Entities support circular and shared references. They can also be versioned.


An entity's persistent state consists of references to other entities and instances of value types. Values are primitives: collections (not what is inside a collection), components and certain immutable objects. Unlike entities, values in particular collections and components, are persisted and deleted by reachability. Since value objects and primitives are persisted and deleted along with their containing entity, they cannot be independently versioned. Values have no independent identity, so they cannot be shared by two entities or collections.


Until now, we have been using the term "persistent class" to refer to entities. We will continue to do that. Not all user-defined classes with a persistent state, however, are entities. A component is a user-defined class with value semantics. A Java property of type java.lang.String also has value semantics. Given this definition, all types (classes) provided by the JDK have value type semantics in Java, while user-defined types can be mapped with entity or value type semantics. This decision is up to the application developer. An entity class in a domain model will normally have shared references to a single instance of that class, while composition or aggregation usually translates to a value type.


We will revisit both concepts throughout this reference guide.


The challenge is to map the Java type system, and the developers' definition of entities and value types, to the SQL/database type system. The bridge between both systems is provided by Hibernate. For entities, <class>, <subclass> and so on are used. For value types we use <property>, <component>etc., that usually have a type attribute. The value of this attribute is the name of a Hibernate mapping type. Hibernate provides a range of mappings for standard JDK value types out of the box. You can write your own mapping types and implement your own custom conversion strategies.


With the exception of collections, all built-in Hibernate types support null semantics.

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!