{"id":2557,"date":"2017-11-21T07:02:22","date_gmt":"2017-11-21T07:02:22","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=2557"},"modified":"2017-11-21T09:23:41","modified_gmt":"2017-11-21T09:23:41","slug":"spring-and-jpa-integration","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/spring-and-jpa-integration\/","title":{"rendered":"Spring and JPA integration"},"content":{"rendered":"<p><strong>application-context.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n  xmlns:p=&quot;http:\/\/www.springframework.org\/schema\/p&quot;\r\n  xmlns:context=&quot;http:\/\/www.springframework.org\/schema\/context&quot;\r\n  xmlns:tx=&quot;http:\/\/www.springframework.org\/schema\/tx&quot;\r\n  xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n  xmlns:aop=&quot;http:\/\/www.springframework.org\/schema\/aop&quot;\r\n  xmlns:jdbc=&quot;http:\/\/www.springframework.org\/schema\/jdbc&quot;\r\n  xsi:schemaLocation=&quot;\r\n   http:\/\/www.springframework.org\/schema\/beans\r\n   http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\r\n   http:\/\/www.springframework.org\/schema\/aop\r\n   http:\/\/www.springframework.org\/schema\/aop\/spring-aop.xsd\r\n   http:\/\/www.springframework.org\/schema\/tx\r\n   http:\/\/www.springframework.org\/schema\/tx\/spring-tx-3.0.xsd\r\n   http:\/\/www.springframework.org\/schema\/jdbc\r\n   http:\/\/www.springframework.org\/schema\/jdbc\/spring-jdbc-3.0.xsd\r\n   http:\/\/www.springframework.org\/schema\/context\r\n   http:\/\/www.springframework.org\/schema\/context\/spring-context-3.0.xsd&quot;&gt;  \r\n   \r\n&lt;context:component-scan base-package=&quot;mugil.org.*&quot; \/&gt;      \r\n&lt;tx:annotation-driven transaction-manager=&quot;transactionManager&quot;\/&gt;\r\n  \r\n&lt;bean id=&quot;transactionManager&quot;\r\n  class=&quot;org.springframework.orm.jpa.JpaTransactionManager&quot;&gt;\r\n     &lt;property name=&quot;entityManagerFactory&quot; ref=&quot;entityManagerFactory&quot; \/&gt;\r\n&lt;\/bean&gt;\r\n\r\n&lt;bean id=&quot;entityManagerFactory&quot;\r\nclass=&quot;org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean&quot;\r\np:dataSource-ref=&quot;dataSource&quot; p:persistenceUnitName=&quot;simple-jpa&quot;&gt;\r\n &lt;\/bean&gt;\r\n  \r\n &lt;bean id=&quot;dataSource&quot; class=&quot;org.springframework.jdbc.datasource.DriverManagerDataSource&quot;&gt;\r\n        &lt;property name=&quot;driverClassName&quot; value=&quot;com.mysql.jdbc.Driver&quot;\/&gt;\r\n        &lt;property name=&quot;url&quot; value=&quot;jdbc:mysql:\/\/localhost:3306\/MUSIC_STORE&quot;\/&gt;\r\n        &lt;property name=&quot;username&quot; value=&quot;music_store&quot;\/&gt;\r\n        &lt;property name=&quot;password&quot; value=&quot;music_store&quot;\/&gt;  \r\n &lt;\/bean&gt;     \r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>Dissection of application-context.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n &lt;context:component-scan base-package=&quot;mugil.org.*&quot; \/&gt;\r\n<\/pre>\n<ol>\n<li>This component-scan tag also enables the usage of JPA annotations.<\/li>\n<li>Enables the usage of JPA annotations(@Service, @Component, @Repository, @Controller  and etc.,)<\/li>\n<li>annotation-config \u2014> enables the usage of JPA annotations , only in the beans specified in the context.xml whereas (ii) component-scan \u2014> scans through all the packages specified, records all the  project beans having JPA annotations, into this context.xml. Therefore it enables JPA annotaion usage in (beans specified in context.xml)+(Project Beans).Inshort, component-scan extends annotation-config.<\/li>\n<\/ol>\n<p><strong>Note:<\/strong><br \/>\nWhen we use component-scan in the app-context.xml, it\u2019s not necessary to use  annotation-config again.Even if both the tags are specified, it\u2019s not a problem because, the spring container would take care of running the process only once. <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n &lt;tx:annotation-driven transaction-manager=&quot;transactionManager&quot;\/&gt;\r\n<\/pre>\n<ol>\n<li>It checks for @Transactional annotation  in any of the classes. This tag is like a Transactional Switch that turns on the transactional behaviour.Here Transaction Manager is being injected<\/li>\n<\/ol>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;bean id=&quot;transactionManager&quot; class=&quot;org.springframework.orm.jpa.JpaTransactionManager&quot;&gt;\r\n   &lt;property name=&quot;entityManagerFactory&quot; ref=&quot;entityManagerFactory&quot; \/&gt;\r\n&lt;\/bean&gt;\r\n\r\n &lt;bean id=&quot;dataSource&quot; class=&quot;org.springframework.jdbc.datasource.DriverManagerDataSource&quot;&gt;\r\n        &lt;property name=&quot;driverClassName&quot; value=&quot;com.mysql.jdbc.Driver&quot;\/&gt;\r\n        &lt;property name=&quot;url&quot; value=&quot;jdbc:mysql:\/\/localhost:3306\/MUSIC_STORE&quot;\/&gt;\r\n        &lt;property name=&quot;username&quot; value=&quot;music_store&quot;\/&gt;\r\n        &lt;property name=&quot;password&quot; value=&quot;music_store&quot;\/&gt;  \r\n &lt;\/bean&gt;     \r\n<\/pre>\n<ol>\n<li>Here Transaction Manager is setup.EntityManagerFactory is being injected.<\/li>\n<li>Here EntityManagerFactory is setup .Data Source reference and Persistence UnitName Reference is specified.\n<\/li>\n<li>DB connection details are specified.<\/li>\n<li>Based on the PersistenceUnit name, the corresponding portion of persistence.xml is accessed (a Project can have multiple persistenceUnitNames)<\/li>\n<\/ol>\n<p><strong>persistence-context.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;persistence version=&quot;2.0&quot; xmlns=&quot;http:\/\/java.sun.com\/xml\/ns\/persistence&quot; xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; \r\n             xsi:schemaLocation=&quot;http:\/\/java.sun.com\/xml\/ns\/persistence http:\/\/java.sun.com\/xml\/ns\/persistence\/persistence_2_0.xsd&quot;&gt;\r\n  &lt;persistence-unit name=&quot;simple-jpa&quot; transaction-type=&quot;RESOURCE_LOCAL&quot;&gt;\r\n    &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;\/provider&gt;\r\n    &lt;class&gt;mugil.pojo.MusicDetails&lt;\/class&gt;\r\n    &lt;exclude-unlisted-classes&gt;true&lt;\/exclude-unlisted-classes&gt;\r\n    &lt;properties&gt;\r\n      &lt;property name=&quot;hibernate.dialect&quot; value=&quot;org.hibernate.dialect.MySQLDialect&quot;\/&gt; \r\n      &lt;property name=&quot;hibernate.show_sql&quot; value=&quot;true&quot;\/&gt;\r\n      &lt;property name=&quot;hibernate.max_fetch_depth&quot; value=&quot;3&quot;\/&gt; \r\n    &lt;\/properties&gt;\r\n  &lt;\/persistence-unit&gt;\r\n&lt;\/persistence&gt;\r\n<\/pre>\n<p><strong>Dissection of persistence-context.xml<\/strong><\/p>\n<ol>\n<li>persistence-unit &#8211; Defines the Name of the Persistence Unit<\/li>\n<li>provider &#8211; ORM tool by which the underlying persistence would be accessed<\/li>\n<li>class &#8211; Entity Class Names<\/li>\n<li>properties &#8211; Defining the underlying persistence technology and other properties would be configured here<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2017\/11\/tx-annotation2-1.png\" alt=\"\" height=\"491\" width=\"556\"\/><\/p>\n<p><strong>Other Java Codes<\/strong><br \/>\n<strong>DaoInterface.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic interface IMusicStoreDao \r\n{\r\n   public List getMusicList();\r\n}\r\n<\/pre>\n<p><strong>DaoImplementation.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Service(value = &quot;MusicCollections&quot;)\r\n@Repository(value = &quot;MusicCollections&quot;)\r\n@Transactional\r\npublic class MusicStoreDaoImpl implements IMusicStoreDao{\r\n \r\n  @PersistenceContext(unitName = &quot;simple-jpa&quot;)\r\n    private EntityManager entityManager;\r\n \r\n   @Override\r\n     public List getMusicList(){\r\n     List musicDetailsList= entityManager.createQuery(&quot;select c from MusicDetails c&quot;).getResultList();\r\n     return musicDetailsList;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Executer.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Executer {\r\n   public static void main(String&#x5B;] args){\r\n        ApplicationContext applicationContext=new ClassPathXmlApplicationContext(&quot;META-INF\/app-context.xml&quot;);\r\n        IMusicStoreDao musicStoreDao=(IMusicStoreDao) applicationContext.getBean(&quot;MusicCollections&quot;);\r\n        System.out.println(&quot;MusicList: \\n&quot;+musicStoreDao.getMusicList());\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Reference:<\/strong><br \/>\n<a href=\"https:\/\/aishwaryavaishno.wordpress.com\/2013\/07\/25\/spring-jpa-example-with-exceptionerror-fixing\/\">Spring and JPA<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>application-context.xml &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt; &lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot; xmlns:p=&quot;http:\/\/www.springframework.org\/schema\/p&quot; xmlns:context=&quot;http:\/\/www.springframework.org\/schema\/context&quot; xmlns:tx=&quot;http:\/\/www.springframework.org\/schema\/tx&quot; xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xmlns:aop=&quot;http:\/\/www.springframework.org\/schema\/aop&quot; xmlns:jdbc=&quot;http:\/\/www.springframework.org\/schema\/jdbc&quot; xsi:schemaLocation=&quot; http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd http:\/\/www.springframework.org\/schema\/aop http:\/\/www.springframework.org\/schema\/aop\/spring-aop.xsd http:\/\/www.springframework.org\/schema\/tx http:\/\/www.springframework.org\/schema\/tx\/spring-tx-3.0.xsd http:\/\/www.springframework.org\/schema\/jdbc http:\/\/www.springframework.org\/schema\/jdbc\/spring-jdbc-3.0.xsd http:\/\/www.springframework.org\/schema\/context http:\/\/www.springframework.org\/schema\/context\/spring-context-3.0.xsd&quot;&gt; &lt;context:component-scan base-package=&quot;mugil.org.*&quot; \/&gt; &lt;tx:annotation-driven transaction-manager=&quot;transactionManager&quot;\/&gt; &lt;bean id=&quot;transactionManager&quot; class=&quot;org.springframework.orm.jpa.JpaTransactionManager&quot;&gt; &lt;property name=&quot;entityManagerFactory&quot; ref=&quot;entityManagerFactory&quot; \/&gt; &lt;\/bean&gt; &lt;bean id=&quot;entityManagerFactory&quot; class=&quot;org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean&quot; p:dataSource-ref=&quot;dataSource&quot; p:persistenceUnitName=&quot;simple-jpa&quot;&gt; &lt;\/bean&gt; &lt;bean id=&quot;dataSource&quot; class=&quot;org.springframework.jdbc.datasource.DriverManagerDataSource&quot;&gt; &lt;property name=&quot;driverClassName&quot; value=&quot;com.mysql.jdbc.Driver&quot;\/&gt; &lt;property name=&quot;url&quot; value=&quot;jdbc:mysql:\/\/localhost:3306\/MUSIC_STORE&quot;\/&gt; &lt;property name=&quot;username&quot; value=&quot;music_store&quot;\/&gt; &lt;property&hellip; <a href=\"https:\/\/codethataint.com\/blog\/spring-and-jpa-integration\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[235],"tags":[],"class_list":["post-2557","post","type-post","status-publish","format-standard","hentry","category-jpa"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2557","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=2557"}],"version-history":[{"count":15,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2557\/revisions"}],"predecessor-version":[{"id":2574,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2557\/revisions\/2574"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=2557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=2557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=2557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}