Using Hibernate for powerful object persistence and querying

In this recipe, you will learn how to use Hibernate with Spring. We'll use a MySQL database.

Getting ready

In this recipe, we'll use a MySQL database with the user table:

CREATE TABLE user (
  id int NOT NULL AUTO_INCREMENT,
  first_name text,
  age int DEFAULT NULL,
  PRIMARY KEY (id)
);

We'll use this corresponding JPA-annotated domain class:

@Entity
@Table(name = "user")
public class User {

  @Id
  @GeneratedValue
  private Long id;
  
  @Column(name = "first_name")
  private String firstName;
  
  private Integer age;
  
  // getters and setters.. 

For more information about the Java Persistence API (JPA), go to: http://docs.oracle.com/javaee/6/tutorial/doc/bnbpz.html.

How to do it…

Here are the steps to integrate Hibernate ...

Get Spring Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.