hibernate

Mappings in Hibernate3 - Quick Reference

An entity class in hibernate can refer to other entity classes or embed value types. An entity is a type on its own and has an existence of its own like a Course and its students. A value type is a type which doesn't have existence of its own, but belongs to an entity like a user and his address. Value types are always completely owned by their containing entity. Once you delete the entity, its associated value types are also deleted. But when you delete an entity, referenced entities still exist: when you delete a Course entity, you don't delete all its students.

Many-to-Many Mapping in Hibernate3

When mapping two entities, we annotate both classes as @Entity and the mapping type is specified as @OneToOne, @OneToMany, @ManyToOne or @ManyToMany over the declaration of one entity class in the other entity class. In Many-To-Many mapping, we refer to a collection/list of the entity from another entity and vice versa; and used @ManyToOne annotation on declarations of both entity collections.

One-to-Many and Many-to-One Mapping in Hibernate3

When mapping two entities, we annotate both classes as @Entity and the mapping type is specified as @OneToOne, @OneToMany, @ManyToOne or @ManyToMany over the declaration of one entity class in the other entity class. In One-To-Many mapping, we refer to a collection/list of the entity from another entity and mark the reference variable as @OneToMany. The opposite of this relation from the second entity to the first will be Many-To-One and is annotated as @ManyToOne. The example will make things more clear.

One-to-One Mapping in Hibernate3

When mapping two entities, we annotate both classes as @Entity and the mapping type is specified as @OneToOne, @OneToMany, @ManyToOne or @ManyToMany over the declaration of one entity class in the other entity class. In One-To-One mapping, we refer to one entity from another and mark the reference variable as @OneToOne.

 

Eager and Lazy Fetch Types in Hibernate3

Consider a user class with an embedded list of addresses, or a company class with a list of employee details. By default, when you load the embedding class (user or company) using session.get, the embedded collection (addresses or employee details) is not actually retrieved from database, but only the top level fields are retrieved. The collection list is retrieved when you call the getter for that embedded collection. This is called lazy initialization fetch in hibernate. The alternative is called eager initialization fetch.

Modifying the Attributes of the Embedded Class in Hibernate3

There might be situations where you would want to modify the attributes of the embedded class (eg. Address) within an embedding class (eg. User), for instance, changing the database column name for a field. You can do this within the embedded class itself. But what if you don’t have access to the embedded class or if you want different behavior for two different instances of the embedded class; you will have to override the embedded class behavior in the embedding class.

Embedding Value Types Within an Entity Class in Hibernate3

An entity class can refer to other entity classes or embed value types. An entity is a type on its own and has an existence of its own like a Course and its students. A value type is a type which doesn't have existence of its own, but belongs to an entity like an user and his address. Value types are always completely owned by their containing entity. Once you delete the entity, its associated value types are also deleted. But when you delete an entity, referenced entities still exist: when you delete a Course entity, you don't delete all its students.

Most Commonly Used Annotations in Hibernate3

Here we will discuss most commonly used basic annotations in hibernate3 - @Entity, @Table, @Id, @GeneratedValue, @Basic, @Transient, @Temporal and @Lob.

 

@Entity

@Entity annotation (javax.persistence.Entity/org.hibernate.annotations.Entity) over the class tell hibernate to treat this class as our entity class that needs to be saved.

 

@Entity (name=”XYZ”)

Your First Hibernate3 Program

We will write a simple hibernate3 application to get started with hibernate3.

Before starting, you should:

Downloading Hibernate 4 Jar Files

Hibernate 4 jars can be downloaded from the SourceForge web site at http://sourceforge.net/projects/hibernate/files/hibernate4.  If this url doesn’t work or get changed, just search ‘hibernate 4 jars download sourceforge’ in Google.  The distribution version downloaded here is 4.2.4. Except for the version numbers, the jar names and folder structure should be same across any version of hibernate4 distribution.  

 

Pages

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!