hibernate

Logging in Hibernate 4.3

You have different logging options in Hibernate and let us see some of those options quickly. Hibernate supports simple property based log enabling to supporting standard logging frameworks and even programmatically accessing log statistics.

 

Configuring a Hibernate 4.3 Application with JPA Entity Managers

In JPA, the EntityManager controls the lifecycle of the entities.

Different ORM vendors provide its own entity manager, which is usually a wrapper around the core API and thus supports the JPA programming interfaces, life cycles and query language.

You obtain the EntityManager from an entity manager factory. EntityManagerFactory has the same role as SessionFactory in Hibernate.

Advance Hibernate 4.3

This section will contain notes on advance topics in Hibernate such as caching, transaction management, concurrency etc. We will also see integrations with Spring with/without using JPA. This note assumes that you are already familiar with the contents provided @ getting-started-with-hibernate-43.

Different Ways to Configure in Hibernate 4.3

Before working with hibernate in your code, you will need to configure Hibernate with information regarding your database such as host, port, credentials etc. You also need to create mappings that tell hibernate how to map the state of a Java entity to the columns of its corresponding table.

We can configure various hibernate features either through annotations or through xml files (hbm files). Preferred approach currently is annotations.

 

You can configure Hibernate mainly in three ways:

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.

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.

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.

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.

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

Pages

Search the Web

Custom Search

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