[JAVA] How to define multiple orm.xml in Spring4, JPA2.1

Introduction

The definition method when managing multiple orm.xml (user.xml, userImage.xml) is described below. Orm.xml in this article means a file that defines entity-mappings such as entity and native-sql. If you search, there is a lot of information on how to define META-INF / persistence.xml and META-INF / orm.xml. This is the definition when using JPA alone, so a different definition is required when using it in combination with Spring4.

Package configuration


-Java
-Resources
     |
     |_conf
     | |_☓☓☓
     | |_☓☓☓
     |
     |_sql
     | |_user.xml
     | |_userImage.xml
     |
     |_applicationContext.xml

Premise

This time, we will use the following version. spring-data-jpa: 1.10.5.RELEASE hibernate-entitymanager: 5.1.0.Final

--maven definition

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.10.5RELEASE</version>
        </dependency>
        <!-- JPA -provider(Hibernate) -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.1.0.Final</version>
        </dependency>

applicationContext.xml definition

The following is a definition example of applicationContext.xml. You can also create a file such as dataSource.xml and import it for all settings for DB connection. To define multiple orm.xml, use mappingResources of property. Define the path under the root of the created orm.xml (user.xml, userImage.xml this time). If you want to increase orm.xml, you need to add it below.

As an aside, if you use only one orm.xml, you can define the file name orm.xml directly under the META-INF directory. , It can be read without the definition of mappingResource below.

Definition example


<jpa:repositories base-package="com.sample.orm.dao"/>
<context:property-placeholder
        location="classpath:dataSource.properties"/>
<!-- mysql -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driver.class}"/>
    <property name="url">
        <value>${jdbc.connect}://${jdbc.host.port}/${jdbc.schema}?useUnicode=true&amp;characterEncoding=UTF-8&amp;connectionCollation=utf8mb4_general_ci</value>
    </property>
    <property name="username" value="${jdbc.user}"/>
    <property name="password" value="${jdbc.pass}"/>
    <property name="maxActive" value="20"/>
    <property name="maxIdle" value="1"/>
</bean>

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

<bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <!--entity directory definition-->
    <property name="packagesToScan" value="com.sample.orm.entity"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="false"/>
            <property name="generateDdl" value="false"/>
            <property name="database" value="MYSQL"/>
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        </props>
    </property>
    <!--Define mapping path-->
    <property name="mappingResources">
        <list>
            <value>sql/user.xml</value>
            <value>Sql/userImage.xml</value>
        </list>
    </property>
</bean>    

Recommended Posts

How to define multiple orm.xml in Spring4, JPA2.1
How to use Lombok in Spring
[How to install Spring Data Jpa]
How to execute multiple commands in docker-compose.yml
How to add a classpath in Spring Boot
How to create a Spring Boot project in IntelliJ
How to use CommandLineRunner in Spring Batch of Spring Boot
How to test file upload screen in Spring + Selenium
How to use In-Memory Job repository in Spring Batch
How to change application.properties settings at boot time in Spring boot
How to call and use API in Java (Spring Boot)
How to control transactions in Spring Boot without using @Transactional
How to unit test Spring AOP
How to run JUnit in Eclipse
How to iterate infinitely in Ruby
How to use Spring Data JDBC
[Rails] How to write in Japanese
How to run Ant in Gradle
How to master programming in 3 months
How to set Spring Boot + PostgreSQL
How to learn JAVA in 7 days
How to get parameters in Spark
How to install Bootstrap in Ruby
How to use ModelMapper (Spring boot)
How to introduce jQuery in Rails 6
How to use classes in Java?
How to name variables in Java
How to set Lombok in Eclipse
[Swift] How to replace multiple strings
How to concatenate strings in java
How to install Swiper in Rails
How to set and use profile in annotation-based Configuration in Spring framework
How to call multiple names at once in the same category
[swift5] How to specify color in hexadecimal
How to implement search functionality in Rails
How to implement Kalman filter in Java
How to change app name in rails
How to get date data in Ruby
How to use custom helpers in rails
How to read Body of Request multiple times with Spring Boot + Spring Security
How to implement a slideshow using slick in Rails (one by one & multiple by one)
Note to have multiple values in HashMap
How to reflect seeds.rb in production environment
How to use named volume in docker-compose.yml
How to filter JUnit Test in Gradle
Convert request parameter to Enum in Spring
How to insert a video in Rails
How to standardize header footer in Thymeleaf
Exists using Specification in Spring Data JPA
How to add jar file in ScalaIDE
[Swift] How to fix Label in UIPickerView
How to use Docker in VSCode DevContainer
How to use MySQL in Rails tutorial
How Dispatcher servlet works in Spring MVC
How to define an inner class bean
How to embed Janus Graph in Java
[rails] How to configure routing in resources
To write Response data directly in Spring
How to map tsrange type in Hibernate
How to get the date in java
How to not start Flyway when running unit tests in Spring Boot