..


Sponsored Links

Data Access - Creating DAO and transaction management

Referring to the example of the preceding paragraph, we see how to create the DAO to the entity Car.

First of all creaimo an interface with methods based on a DAO, namely: load, save, update and deletion.






 it.mrwebmaster.hibernate.dao package;









 it.mrwebmaster.hibernate.Car imports;









 {public interface CarDao





  



 public car load (Integer id);



	

  



 save public Car (Car car);



	

  



 update public Car (Car car);



	

  



 public void remove (Car car);







 }



Created interface pass in the creation of implementation:





 it.mrwebmaster.hibernate.dao package;









 it.mrwebmaster.hibernate.Car imports;









 javax.persistence.EntityManager imports;







 javax.persistence.PersistenceContext imports;









 org.springframework.transaction.annotation.Transactional imports;









 @ Transactional







 CarDaoImpl {public class implements CarDao





  



 @ PersistenceContext (unitName = "can")



  



 private EntityManager em;



	

  



 @ Override



  



 public void remove (Car car) {



    



 em.remove (char);



  



 }





  



 @ Override



  



 @ Transactional (readOnly = true)



  



 public car load (Integer id) {



    



 return em.find (Car.class, id);



  



 }





  



 @ Override



  



 save public Car (Car car) {



    



 em.persist (char);



    



 return car;



  



 }





  



 @ Override



  



 update public Car (Car car) {



    



 em.merge return (char);



  



 }





  



 public void setEm (EntityManager em) {



    



 this.em = em;



  



 }





  



 getEm public EntityManager () {



    



 return p;



  



 }







 }



and finally in 'applicationContext.xml:





 <! - CAR DAO ->







 <bean id="carDaoImpl" class="it.mrwebmaster.hibernate.dao.CarDaoImpl" />



As can be seen to implement the DAO nost we need EntityManager, which is injected directly from us' through the use of IoC annotations @ PersistenceContext.

The implementations of each method are trivial, simply delegate the work all'EntityManager. As for transaction management, has chosen to use the @ Transactional annotation. This allows Spring to create the instance of our DAO will use a proxy, whose job is to open and close the transaction before the call to a method and immediately after the 'exit from the method itself doing a commit or a rollback in case of error.
Note, finally, the use of the @ Transactional for ReadOnly nell'annotation load method, in so doing has created a transition read-only.

This transaction management is done through the use of AOP, to enable it just add to our applicazionContext.xml:






 <! - Transaction Management ->







 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">



  



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







 </ Bean>







 <tx:annotation-driven transaction-manager="transactionManager" />



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