Video Script ORM Latest 11 9
Video Script ORM Latest 11 9
3th video==
Now the boilerplate code in the addEmployee is removed. We can do the same in
other DAO methods as well.
So, Let us go to DAO class. We can place the @Transactional annotation in the
class level as well. So that all the methods transaction will be managed implicitly.
It means before the method execution starts transaction starts, after successful
execution of the method, transaction gets committed.
Now we can remove the code snippets of creating the entity manager, creating
transaction, managing the transaction using begin and commit methods.
4th video.
I am adding Transactional annotation at class level in DAO implementation class.
In the value attribute of this annotation, we will assign custom transaction
manager name which is txManager .we will create custom transaction manager
Later in jpa spring configuration file.
So, whenever any curd method is going to execute automatically, the transaction
begins, and once method executed transaction will commit automatically. Now
we can comment or remove begin and commit method from add employee
method.
Now we can see we have removed a lot of boilerplate code from the add
employee method.
Now we need to configure springjpa.xml file which is written in resources
package. in this file we must instantiate jpa transaction manager bean
And create a custom transaction manager TX manager. for doing so we need to
inject entity manager factory in jpa transaction manager bean. Now we open a
<bean id="txManager"
class="org.springframework.orm.jpa.JpaTransactionManage
r"> <property name="entityManagerFactory
ref="entityManagerFactory" />
</bean>
bean tag and in id attribute we assign the name of custom transaction manager
which is TX manager. in the class attribute we will assign JpaTranscatonManager.
Within bean tag we will open a property tag. in the property tag name attribute,
we assign entity manager factory and in the reference attribute we assign
reference of entity manager factory which is entityManagerFactory
Now we have created a custom transaction manager. Now we open annotation.
<tx:annotation-driven transaction-manager="txManager"/>