Skip to content
July 7, 2010 / damianmigo

Tutorial: JPA + Hibernate + Spring ORM (usando Eclipse)

Requisitos:

7/13/2010: Anexo el código fuente para descargar

Pasos:

1.- Crear el proyecto

  • Proyect Explorer (clic derecho) -> New -> Project… -> Web -> Dynamic Web Project -> Next
  • Project name: com.example
  • Target runtime: Apache Tomcat v6.0
  • Finish

Pantalla1 

  • Project Explorer -> com.example (clic derecho) -> Properties -> Project Facets -> Java persistence (checar) -> Futher configuration available…

Pantalla2

  • Plafform: Generic
  • JPA implementation, Type: Disable Library Configuration
  • Connection: <None>
  • Persistent class management, Annotated classes must be listed in persistence.xml (checar)
  • Create orm.xml (checar)
  • OK
  • OK

Pantalla3

  • Arrastrar las siguientes líbrerías al directorio com.example\Java Resources: src\WebContent\WEB-INF\lib:

hibernate-distribution-3.5.1-Final\lib\required\

antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
slf4j-api-1.5.8.jar

hibernate-distribution-3.5.1-Final\lib\jpa\

hibernate-jpa-2.0-api-1.0.0.Final.jar

hibernate-distribution-3.5.1-Final\

hibernate3.jar

hibernate-distribution-3.5.1-Final\lib\bytecode\cglib\

cglib-2.2.jar

slf4j-1.5.11\

slf4j-simple-1.5.11.jar

spring-framework-2.5\dist\

spring.jar

mysql-connector-java-5.1.12\

mysql-connector-java-5.1.12-bin.jar

commons-logging-1.1.1\

commons-logging-1.1.1.jar

asm-3.2\lib\

asm-3.2.jar

2.- Crear los POJOs

  • Project Explorer -> com.example -> Java Resources: src (clic derecho) -> New -> Package
  • Name: com.example.domain
  • Finish
  • Project Explorer -> com.example ->Java Resources: src (clic derecho) -> New -> Class
  • Package: com.example.domain
  • Name: User
  • Finish
  • Código fuente de la clase User:
package com.example.domain;  
  
import static javax.persistence.CascadeType.ALL;  
import static javax.persistence.GenerationType.AUTO;  
  
import java.util.Collection;  
  
import javax.persistence.Column;  
import javax.persistence.Entity;  
import javax.persistence.GeneratedValue;  
import javax.persistence.Id;  
import javax.persistence.OneToMany;  
import javax.persistence.Table;  
  
@Entity  
@Table(name = "USERS")  
public class User {  
  
  @Id  
  @GeneratedValue(strategy = AUTO)  
  @Column(name = "ID_USER")  
  private Integer id;  
    
  @Column(name = "USERNAME", nullable = false)  
  private String username;  
    
  @Column(name = "PASSWORD", nullable = false)  
  private String password;  
    
  @Column(name = "EMAIL", nullable = true)  
  private String email;  
  
  @OneToMany(cascade = ALL, mappedBy = "user")  
  private Collection<Photo> photos;  
    
  { ... Getters y Setters ... }  
    
}
  • Project Explorer -> com.example -> Java Resources: src (clic derecho) -> New -> Class
  • Package: com.example.domain
  • Name: Photo
  • Finish
  • Código fuente de la clase Photo:
package com.example.domain;  
   
import static javax.persistence.GenerationType.AUTO;  
   
import javax.persistence.Basic;  
import javax.persistence.Column;  
import javax.persistence.Entity;  
import javax.persistence.FetchType;  
import javax.persistence.GeneratedValue;  
import javax.persistence.Id;  
import javax.persistence.JoinColumn;  
import javax.persistence.Lob;  
import javax.persistence.ManyToOne;  
import javax.persistence.Table;  
   
@Entity  
@Table(name = "PHOTOS")  
public class Photo {  
     
  @Id  
  @GeneratedValue(strategy = AUTO)  
  @Column(name = "ID_PHOTO")  
  private Integer id;  
     
  @ManyToOne  
  @JoinColumn(name = "ID_USER", nullable = false)  
  private User user;  
     
  @Column(name = "CONTENT_TYPE", nullable = false)  
  private String contentType;  
    
  @Basic(fetch = FetchType.LAZY)  
  @Lob  
  @Column(name = "FILE", nullable = false)  
  private byte[] file;  
  
  { ... Getters y Setters ... }
  
}  

3.- Modificar los archivos de configuración de JPA

  • Modificar el contenido del archivo orm.xml localizado en com.example/Java Resources: src/META-INF:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="1.0" xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd">
  <entity class="com.example.domain.User"></entity>
  <entity class="com.example.domain.Photo"></entity>
</entity-mappings>
  • Modificar el contenido del archivo persistence.xml localizado en com.example/Java Resources: src/META-INF:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
  xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="example">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
      <property name="hibernate.hbm2ddl.auto" value="update" />
      <property name="hibernate.show_sql" value="true" />
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
      <property name="hibernate.connection.username" value="root" />
      <property name="hibernate.connection.password" value="password" />
      <property name="hibernate.connection.url" value="jdbc:mysql://localhost/lunar?autoReconnect=true" />
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
      <property name="hibernate.c3p0.min_size" value="5" />
      <property name="hibernate.c3p0.max_size" value="20" />
      <property name="hibernate.c3p0.timeout" value="300" />
      <property name="hibernate.c3p0.max_statements" value="50" />
      <property name="hibernate.c3p0.idle_test_period" value="3000" />
    </properties>
  </persistence-unit>
</persistence> 

4.- Configurar Spring

  • Project Explorer –> Java Resources: src (click derecho) –> New –> XML
  • Selecciona la carpeta com.example/src
  • Filename: beans-jpa.xml
  • Finish
  • Contenido del archivo beans-jpa.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<tx:annotation-driven />
	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
		<property name="persistenceUnitName" value="example" />
	</bean>
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>
	<bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>
	<bean name="photoDao" class="com.example.domain.dao.PhotoDao">
		<property name="jpaTemplate" ref="jpaTemplate" />
	</bean>
	<bean name="userDao" class="com.example.domain.dao.UserDao">
		<property name="jpaTemplate" ref="jpaTemplate" />
	</bean>
</beans>

5.- Crear los DAOs (Data Access Objects)

  • Project Explorer –> Java Resources: src (clic derecho) –> New –> Class
  • Package: com.example.domain.dao
  • Class: GenericDao
  • Finish
  • Contenido de la clase GenericDao:
package com.example.domain.dao;

import java.io.Serializable;
import java.util.List;

import org.springframework.orm.jpa.JpaTemplate;
import org.springframework.transaction.annotation.Transactional;

public abstract class GenericDao<T, ID extends Serializable>  {

  private JpaTemplate jpaTemplate;
  
  private Class<T> persistentClass;
  
  public GenericDao(Class<T> persistentClass) {
    this.persistentClass = persistentClass;
  }
  
  public void setJpaTemplate(JpaTemplate jpaTemplate) {
    this.jpaTemplate = jpaTemplate;
  }
  
  protected JpaTemplate getJpaTemplate() {
    return jpaTemplate;
  }
  
  @Transactional
  public void merge(T entity) {
    jpaTemplate.merge(entity);
  }
  
  @Transactional
  public void persist(T entity) {
    jpaTemplate.persist(entity);
  }
  
  @Transactional
  public void remove(T entity) {
    jpaTemplate.remove(entity);
  }
  
  @Transactional(readOnly = true)
  public T findById(ID id) {
    return jpaTemplate.find(getPersistentClass(), id);
  }
  
  @SuppressWarnings("unchecked")
  @Transactional(readOnly = true)
  public List<T> findAll() {
    return jpaTemplate.find("from " + getPersistentClass().getSimpleName());
  }
  
  public Class<T> getPersistentClass() {
        return persistentClass;
    }
  
}
  • Project Explorer –> Java Resources: src (clic derecho) –> New –> Class
  • Package: com.example.domain.dao
  • Class: UserDao
  • Finish
  • Contenido de la clase UserDao:
package com.example.domain.dao;

import com.example.domain.User;

public class UserDao extends GenericDao<User, Integer> {
  
  public UserDao() {
    super(User.class);
  }

}
  • Project Explorer –> Java Resources: src (clic derecho) –> New –> Class
  • Package: com.example.domain.dao
  • Class: PhotoDao
  • Finish
  • Contenido de la clase PhotoDao:
package com.example.domain.dao;

import com.example.domain.Photo;

public class PhotoDao extends GenericDao<Photo, Integer> {
  
  public PhotoDao() {
    super(Photo.class);
  }

}
  • Project Explorer –> Java Resources: src (clic derecho) –> New –> Class
  • Package: com.example.domain.dao
  • Class: DaoFactory
  • Finish
  • Contenido de la clase DaoFactory:
package com.example.domain.dao;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DaoFactory {

  private static ApplicationContext applicationContext;

  private static PhotoDao photoDao;
  
  private static UserDao userDao;
  
  static {
    applicationContext = new ClassPathXmlApplicationContext("beans-jpa.xml");
    photoDao = (PhotoDao)applicationContext.getBean("photoDao");
    userDao = (UserDao)applicationContext.getBean("userDao");
  }

  private DaoFactory() {

  }

  public static PhotoDao getPhotoDao() {
    return photoDao;
  }

  public static UserDao getUserDao() {
    return userDao;
  }

}

6.- Crear la base de datos

mysql -h localhost -u root -ppassword
mysql> CREATE DATABASE `example` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

7.- Servlet de prueba

  • Project Explorer –> Java Resources: src –> New –> Servlet
  • Java package: com.example.web
  • Class name: ExampleServlet
  • Finish
  • Código fuente de la clase ExampleServlet:
package com.example.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.domain.User;
import com.example.domain.dao.DaoFactory;
import com.example.domain.dao.UserDao;

public class ExampleServlet extends HttpServlet {
  
  private static final long serialVersionUID = 1L;

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    UserDao userDao = DaoFactory.getUserDao();
    
    User user1 = new User();
    user1.setEmail("john.doe@yahoo.com");
    user1.setPassword("d41d8cd98f00b204e9800998ecf8427e");
    user1.setUsername("john");    
    userDao.persist(user1);
    
    User user2 = new User();
    user2.setEmail("juan.perez@hotmail.com");
    user2.setPassword("d41d8cd98f00b204e9800998ecf8427e");
    user2.setUsername("jperez");    
    userDao.persist(user2);
    
    User user3 = new User();
    user3.setEmail("arnulfo@gmail.com");
    user3.setPassword("d41d8cd98f00b204e9800998ecf8427e");
    user3.setUsername("agonzalez");   
    userDao.persist(user3);
    
    PrintWriter printWriter = response.getWriter();
    
    List<User> users = userDao.findAll();
    for (User user : users) {
      printWriter.println(user.getEmail());
    }
  }

}
About these ads

4 Comments

Leave a Comment
  1. bernardo / Mar 15 2012 3:03 pm

    Excelente tutorial, el unico detalle es que no aparece enunciada la configuracion del servlet en el web.xml. Quiza a alguien le dio ese error.

  2. Juan Sin Miedo / Jun 5 2012 1:52 am

    Buen tuto, he hecho una leída diagonal y me lo guardo en favoritos para hacerlo entero esta tarde. Un saludo.

  3. alex / Nov 14 2012 1:18 pm

    excelente me ayudar mucho ……pero tienes el codigo fuente.?

    • damianmigo / Nov 14 2012 2:28 pm

      Si el código fuente lo puedes descargar de la liga que se encuentra en la parte superior, la de color verde.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: