hibernate

[Example-Lab] Table Per Class Inheritance Strategy in Hibernate 4.3

In the TABLE_PER_CLASS strategy, hibernate will create a separate table for all the classes in an inheritance hierarchy. It is still not normalized completely as inherited columns are repeated in all tables. For instance, Fields inherited from Shape such as id and fillcolor are repeated in all the tables.

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

[Example-Lab] Joined Inheritance Strategy in Hibernate 4.3

In the JOINED strategy, hibernate will create a separate table for all the classes in an inheritance hierarchy, and the tables are normalized. Columns that you receive as part of inheritance will be part of parent class and referred from subclass tables using a foreign key. 

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

[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.  

[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] 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.

Introduction to Hibernate Query Language (HQL) in hibernate 4.3

Hibernate provides a query language called Hibernate Query Language (HQL). HQL is similar to SQL in syntax, but HQL queries are written against Hibernate's entity objects, not database tables. Hibernate also provide Criteria Queries as an object-oriented alternative to HQL. Criteria Query is used to modify the objects and provide the restriction for the objects. Here we will see the basics of HQL and later in another tutorial we will see criteria queries.

Parameter Binding in HQL using hibernate 4.3

Parameter binding is the process of binding a Java variable with an HQL statement. Using Parameter binding and not string concatenation for HQL statement creation will also guard against attacks like SQL injection.

Named Queries in HQL with hibernate 4.3

We can have queries saved with a name and later we can retrieve them simply using that name. We use @NamedQuery annotation to declare a named query. We can also have a named query for native sql. For native SQL we use the annotation @NamedNativeQuery annotation. This is one of the important advantages of having a named query: you can write native SQL. You can retrieve a Query object from a saved query name (hql and native named query) using session.getNamedQuery method.

Querying Using Criteria API in Hibernate 4.3

Hibernate provide Criteria Queries as an object-oriented alternative to HQL. Criteria Query is used to modify the objects and provide restriction for the objects. Many complex queries in HQL or even SQL may become larger over a period of time and spread over many lines; and will be difficult to update and maintain. We have already seen HQL in http://javajee.com/introduction-to-hibernate-query-language-hql-in-hibernate-43.

[Lab] Your First Hibernate 4.3 Program

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

Before starting, you should:

Pages

Search the Web

Custom Search

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