Hibernate Configuration

  • Configuration Object

    • It represents a configuration or properties file required by the Hibernate and usually created only once during application initialization.
    • It provides two keys components:
      • Database Connection: This is handled through configuration file supported by Hibernate hibernate.cfg.xml.
      • Class Mapping Setup: This component creates the connection between the Java classes and database tables.
  • Configuring hibernate.cfg.xml

    • Configuration settings related to DB & other related parameters :Connection Driver, URL, username, password.
    • Defines Mapping information between the Java classes & the database tables.

    Basic O/R Mapping

    • Object/relational mappings are usually defined in an XML document.
    • The mapping document is designed to be readable and hand-editable.
    • The mapping language is Java-centric, meaning that mappings are constructed around persistent class declarations and not table declarations.
    • Hibernate-Mapping
    • < hibernate-mapping 
      	schema="schemaName"
      	catalog="catalogName"
      	default-cascade="cascade_style"
      	default-access="field|property|className"
      	default-lazy="true|false"
      	auto-import="true|false"
      	package="package.name"
      />		
      1. schema : (optional) the name of a database schema.
      2. catalog: (optional)then ame 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 Property Accessor.
      5. default-lazy : (optional-default to true) the default value for unspecified lazy attributes of class and collection mappings .
      6. default-import :(optional-default to true) specifies whether we can unqualified class names of classes in this mapping in the query language.
    • class
    • <class
      	name="ClassName"
      	table="tableName"
      	discriminator-value="discriminator_value"
      	mutable="true|false"
      	schema="owner"
      	catalog="catalog"
      	proxy="proxyInterface"
      	dynamic-update="true|false"
      	dynamic-insert="true|false"
      	select-before-update="true|false"
      	polymorphism="implicit|explicit"
      	where="arbitrary sql where condition"
      	persister="PersisterClass"
      	batch-size="N"
      	optimistic-lock="none|version|dirty|all"
      	lazy="true|false"
      	entity-name="EntityName"
      	check="arbitrary sql check condition"
      	rowid="rowid"
      	subselect="SQL expression"
      	abstract="true|false"
      	node="element-name"
      />			
      1. Name:(optional)the fully qualify java class name of the persistence 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. discriminator-value: (optional-default 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.
      5. schema: (opitonal)overrides the schema name specified by the root<hibernate-mapping> element
      6. catalog:(opitonal) overrides the catalog name specified by the root < hibernate-mapping> element
      7. proxy:(opitonal)Specifies an interface to use the lazy intializing proxies. You can specify the name of the class itself.
      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 can contain only the columns whose values are not null.
      10. select-before-update:(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 transient object has been associated with a new session using update(), will Hibernate perform an extra SQL SELECT to determine if an UPDDATE is actually required.
      11. polymnorphism : (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: (opiotnal)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-look : (optional - defaults to version) determines the optimistic locking strategy.
    • Id, Composite Id and Generators
    • < id
      	name="propertyName"
      	type="typename"
      	column="column_name"
      	unsaved-value="null|any|none|undefined|id_value"
      	access="field|property|ClassName">
      </id>					
      1. name: (optional)the name of the identifier property.
      2. type:(optional)a name that indicates the Hibernate type.
      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.
      5. access: (optional- defaults to property)the strategy Hibernate should use for accessing the property value.
    • Property
    • <property 
      	name="propertyName"
      	column="column_name"
      	type="type name"
      	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"
      />				
      1. name: (optional)the name of the property, with an initial lowercase letter.
      2. colums:(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. type:(optional)a name that indicates the Hibernate type.
      4. update,insert: (optional-defaults to true)Specifies that the mapped columns should be included in SQL UPDATE and/or INSERT statements.
      5. formula: (optional)an SQL expression that defines the value for a computed property. Computed properties do not have a column mapping of their own.
      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.
      8. unique: (optional)enables the DDL generation of a unique constraints for the columns.
      9. not-null: (optional)enables the DDL generation of a nullability constraints 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.
      11. generated:(optional- defaults to never) Specifies that this property value is actually generated by the database.
    • One to one
    • <many-to-one 
      	name="propertyName"
      	class="className"
      	cascade="cascade_style"
      	constrained="true|false"
      	fetch="join|select"
      	property-ref="propertyNameFromAssociatedClass"
      	access="field|property|ClassName"
      	formula="arbitrary SQL expression"
      	lazy="true|false"
      	entity-name="EntityName"
      	node="element-name|@attribute-name|element/@attribute|."
      	embed-xml="true|false"
      	foreign-key="foreign_key_name"
      />
      1. name: (optional)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 bject to the associated object.
      4. constrained:(optional) Specifies that a foreign key constraint on the primary key of the mapped table and reference the table of the associated class.
      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 this foreign key. 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)an SQL expression that defines the value for a computed property. Computed properties do not have a column mapping of their own.
      9. update,insert:(opiotnal-defaults to true) Specifies that the mapped columns should be included in SQL UPDATE and/or INSERT statements.
      10. lazy: (optional-defaults to false)Specifies that this property should be fetched lazily when the instance variable is first accessed.
      11. entity-name:(optional) the entity name of the associated class.
    • Many to one
    • <many-to-one 
      	name="propertyName"
      	column="column_name"
      	class="className"
      	cascade="cascade_style"
      	fetch="join|select"
      	update="true|false"
      	insert="true|false"
      	property-ref="propertyNameFromAssociatedClass"
      	access="field|property|ClassName"
      	unique="true|false"
      	not-null="true|false"
      	optimistic-lock="true|false"
      	lazy="true|false"
      	not-found="true|false"
      	entity-name="EntityName"
      	formula="arbitrary SQL expression"
      />				
      1. name:(optional) the name of the property.
      2. columns:(optional)the name of the foreign key . 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. cascade: (optional)Specifies which operations should be cascaded from the object to the associated object.
      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.
      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 constraints for the columns.
      10. not-null: (optional)enables the DDL generation of a nullability constraints for the columns.
      11. optimistic-lock:(optional -defaults to true) Specifies that updates to this property do or do not require acquisition of the optimistic lock.
      12. lazy: (optional-defaults to false)Specifies that this property should be fetched lazily when the instance variable is first accessed.
      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: (optional)an SQL expression that defines the value for a computed property. Computed properties do not have a column mapping of their own.