..


Sponsored Links

Data Access - Creating an ORM layer using Hibernate and JPA

Now let's see how to create an ORM layer using Hibernate 3 and JPA 2 (for the correct understanding of this lesson, it is assumed that the reader has basic knowledge about the data source and the operation of the Hibernate and JPA).

Suppose we have a DB MySQL installed on our machine, first let's define our applicationContext.xml a data source that connects to our DB:






 <! - DATA SOURCE ->







 <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">



  



 <property name="driverClassName" value="com.mysql.jdbc.Driver" />



  



 <property name="url" value="jdbc:mysql://localhost:3306/mrweb" />



  



 <property name="username" value="mrweb" />



  



 <property name="password" value="s3cret" />







 </ Bean>



Once we create a data source created for JPA persistence unit located in the file META-INF/persistence.xml:





 <? Xml version = "1.0" encoding = "UTF-8"?>







 <Persistence 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_2_0.xsd"



  



 version = "2.0">







 <persistence-unit name="pu" transaction-type="RESOURCE_LOCAL" />







 </ Persistence>



At this point we are ready to integrate with Hibernate and JPA:





 <! - JPA AND HIBERNATE ->







 <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">



  



 <property name="database" value="MYSQL" />



  



 <property name="showSql" value="true" />



  



 <property name="generateDdl" value="true" />







 </ Bean>



Without this our creaimo EntityManagerFactory that will allow us to inject the DAO into our EntityManger:





 <! - FACTORY MANAGER ENTITY ->







 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">



  



 <property name="dataSource" ref="myDataSource" />



  



 <property name="persistenceUnitName" value="pu" />



  



 <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />







 </ Bean>



Entity example we create now a Car:





 it.mrwebmaster.hibernate package;









 import java.util.Date;









 javax.persistence.Column imports;







 javax.persistence.Entity imports;







 javax.persistence.GeneratedValue imports;







 javax.persistence.Id imports;







 javax.persistence.PersistenceUnit imports;









 @ Entity







 @ PersistenceUnit (unitName = "can")







 public class Car {





  



 @ Override



  



 public String toString () {



    



 return "Car [id =" + id + "name =" + name + ", year =" + year + "]";



  



 }





  



 @ Id



  



 @ GeneratedValue



  



 private String id;



	

  



 @ Column



  



 private String name;





  



 @ Column



  



 private Date year;





  



 public Integer getId () {



    



 return id;



  



 }





  



 public String getName () {



    



 return name;



  



 }





  



 public Date getYear () {



    



 return year;



  



 }





  



 public void setId (Integer id) {



    



 this.id = id;



  



 }





  



 public void setName (String name) {



    



 this.name = name;



  



 }



	

  



 public void setYear (Date year) {



    



 this.year = year;



  



 }







 }



Our Entity has only three columns: Id, Name, and Year, where the Id column is our Primary Key that will be generated automatically by Hibernate.
Having specified in our configuration parameter "= true bill", Hibernate will update the tables in our DB automatically.
'S use of the annotation @ PersistenceUnit with this configuration line nell'applicationContext. Xml:





 <! - JPA ANNOTATION ->







 <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />



makes it unnecessary to specify, within the persistence unit, the list of entities in our application.

Spring Java Guide
E-Learning
Linux (Course) Linux (Course)
Complete guide to open-source system. From 49 €.
PHP (Course) PHP (Course)
Full course for creating dynamic Web sites. From 49 €.
Ruby and Ruby on Rails (Course) Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €.
Sponsored Links