hibernate

[Example-Lab] Single Table Inheritance Strategy in Hibernate 4.3

In the SINGLE_TABLE strategy, hibernate will create a single table for all the classes in an inheritance hierarchy. There will be a discriminator column that tells us the class to which that row belongs.

In this example, we will create and save three entity classes – Shape, Rectangle and Circle; where Rectangle and Circle extends from Shape.

Mappings in Hibernate 4.3

When referencing entities from other entities, we are mapping two entities whereas when referencing a value type we are embedding (or containing) a value type within an entity.

When mapping two entities, we annotate both classes as @Entity, and the mapping type is specified using the annotations @OneToOne, @OneToMany, @ManyToOne or @ManyToMany, over the reference variable declaration of an entity in another entity.

We will consider two entities for mapping examples – a student class and a course class.

[Example-Lab] Many-to-Many Mapping in Hibernate 4.3

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.

We will assume that each course can have many students (many to many), and each student can do many course(many to many).

We will create two entity classes, Course and Student and do a many to many mapping from course to student, and again many to many mapping from student to course.

[Example-Lab] One-to-Many and Many-to-One Mapping in Hibernate 4.3

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. 

We will assume that each course can have many students(one to many), but each student can do only one course(many to one).

We will create two entity classes, Course and Student and do a one to one mapping from course to student, and many to one from student to course.

[Example-Lab] One-to-One Mapping in Hibernate 4.3

In One-To-One mapping, we refer to one entity from another and mark the reference variable as @OneToOne.

In this example, we will consider two entity classes – student class and course class; and do a one to one mapping from course to student.

We will assume that there is a one-to-one mapping between a Course and Student – each course can be taken by only one student.  

Eager and Lazy Fetch Types in Hibernate 4.3

When you load an embeding class, it may load its embedded collections either lazily or eagerly.

 

Important points about eager and lazy fetch types in Hibernate

  1. By default, when you load an embedding class (E.g. User or Company) using session.get,

    • its embedded collections (E.g. AddressList or EmployeeList) is not actually retrieved from database, but only the top level fields are retrieved.

[Example-Lab] Modifying the Attributes of the Embedded Class from Embedding Class in Hibernate 4.3

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.

We can use @AttributeOverride annotation to override the details of a field of the embedded class and then enclose all such @AttributeOverride annotations inside @AttributeOverrides annotation.

Modifying the Attributes of the Embedded Class in Hibernate 4.3

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 and Collections Within an Entity Class in Hibernate 4.3

We have already seen the definition and properties of an entity and value class in http://javajee.com/entities-and-value-types-in-hibernate-43. We will not see how we can actually embed a value type within an entity. We will also see how we can embed a collection inside an entity.

 

Most Common Annotations Used in Hibernate 4.3

Here we will discuss most commonly used basic annotations in hibernate - @Entity, @Table, @Id, @GeneratedValue, @Column, @Basic, @Transient, @Temporal and @Lob. Even though many annotations has both JPA (java.persistance) and hibernate (org.hibernate) versions, it is preferred to use JPA annotations whenever possible. As you can see below, some of the hibernate specific annotations are already deprecated.

 

Pages

Search the Web

Custom Search

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