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
This time, we will use the following version. spring-data-jpa: 1.10.5.RELEASE hibernate-entitymanager: 5.1.0.Final
<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>
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&characterEncoding=UTF-8&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