Developing application with friend, but ran into a question... I had this code :
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${jdbc.dialect}</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">create</prop> </props> </property> </bean> <tx:annotation-driven /> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="dataSource" ref="dataSource" /> <property name="sessionFactory" ref="sessionFactory" /> </bean>
And he removed it all, changed it to:
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitName" value="persistenceUnit"/> <property name="dataSource" ref="dataSource"/> </bean>
He said, that this is better approach, but did not exactly explain why. Something about not using hibernate directly, does this mean we are not using hibernate at all anymore? Is it really better approach?
DAO changed like this:
I had:
@Autowired private SessionFactory sessionFactory; public void addUser(User user) { sessionFactory.getCurrentSession().save(user); }
Now is:
@PersistenceContext private EntityManager entityManager; public void addUser(User user) { entityManager.persist(user); }
This transaction manager is appropriate for applications that use a single JPA EntityManagerFactory for transactional data access. JTA (usually through JtaTransactionManager ) is necessary for accessing multiple transactional resources within the same transaction.
This transaction manager is appropriate for applications that use a single Hibernate SessionFactory for transactional data access, but it also supports direct DataSource access within a transaction (i.e. plain JDBC code working with the same DataSource).
JtaTransactionManager enables you to integrate a JTA provider with your Spring application. JtaTransactionManager only facilitates integration of a JTA transaction provider and is not a provider in itself.
The @Transactional annotation makes use of the attributes rollbackFor or rollbackForClassName to rollback the transactions, and the attributes noRollbackFor or noRollbackForClassName to avoid rollback on listed exceptions. The default rollback behavior in the declarative approach will rollback on runtime exceptions.
He wants your code to be dependent on JPA, instead of Hibernate. javax.persistence.EntityManager
is a JPA (Java EE) standard interface, while org.hibernate.SessionFactory
is a Hibernate propertary interface. Moving from SessionFactory to EntityManager makes your classes no longer dependent on Hibernate. Using the JPA-way instead of the Hibernate-way is considered best practice by most people today.
Please note that your system is still dependent upon Hibernate, as you need a JPA provider. But if you want to change to another JPA provider in the future it should be pretty straight forward. Another advantage for going for JPA is that the JPA-interfaces are more stable than the Hibernate ones.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With