Tuesday, 22 March 2016

DataSource Connection with JPA

Below code helps to check the persistence connection

final AbstractApplicationContext context =
TravelRepository repos = (PbfBalanceChangesViewRepository) context.getBean("travelRepository");
TravelView travelView=repos.findByRouteNo("002");

System.out.println(travelView.getAvailBalNew());

aplication-context.xml will look as like below

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd">

  <context:property-placeholder location="file:///${app.configs}/travel/traveldetails.properties" />

<context:annotation-config />
  <context:component-scan base-package="com.travel"/>
  <!-- Repositories -->
  <jpa:repositories base-package="com.travel.repository"
  entity-manager-factory-ref="travelEmf"
  transaction-manager-ref="transactionManager"/>
<!-- Transactions -->
  <tx:annotation-driven transaction-manager="transactionManager" />


  <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="database" value="ORACLE"/>
    <property name="showSql" value="true"/>
    <property name="generateDdl" value="false"/>
    <!-- <property name="databasePlatform" value="org.eclipse.persistence.platform.database.OraclePlatform"/> -->
    <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect"/>
  </bean>

  <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
  <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
  <!-- read: http://stackoverflow.com/questions/9136202/defining-hibernateexceptiontranslator-no-persistence-exception-translators-foun -->
  <bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator"/>

  <jdbc:embedded-database id="dataSource" type="H2">  
<jdbc:script location="classpath:META-INF/sql/travel-schema.sql" />    
  </jdbc:embedded-database>    
 
  <bean id="travelEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
<property name="persistenceXmlLocation" value="classpath:/META-INF/persistence-LOCAL.xml"/>  
    <property name="persistenceUnitName" value="travelPU-LOCAL"/>
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
  </bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="travelEmf" />
</bean>    
 
</beans>

Persistence.xml is as follows

<persistence>
    <persistence-unit name="travelPU" transaction-type="RESOURCE_LOCAL">
        <class>com.travel.TravelDetail</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527 TravelDB"/>
            <property name="javax.persistence.jdbc.user" value="travel_app"/>
            <property name="javax.persistence.jdbc.password" value="travel_app"/>
        </properties>
    </persistence-unit>

</persistence>



No comments:

Post a Comment