hibernate

Your First Hibernate3 Program

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

Before starting, you should:

Mappings in Hibernate - 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.

CRUD Operations and Object States in Hibernate

Here we will see all the CRUD operations, and also object states and transitions in hibernate. CRUD operations are Create(save), Read(select), Update(update) and Delete(delete). We will see object states in hibernate such a new/transient, attached/persistent and detached.

Most Commonly Used Annotations in Hibernate

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

 

@Entity

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

Embedding Value Types Within an Entity Class in Hibernate

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.

Modifying the Attributes of the Embedded Class in Hibernate

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.

One-to-Many and Many-to-One Mapping in Hibernate

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.

Eager and Lazy Fetch Types in Hibernate

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.

Many-to-Many Mapping in Hibernate

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-One Mapping in Hibernate

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.

 

Pages

Search the Web

Custom Search

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