1. เริ่มจากเอา Config JPA ออกจาก SpringBoot ก่อนครับ โดยใช้ exclusion org.hibernate
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
</exclusion>
</exclusions>
</dependency>
2.ต่อไปก็เพิ่ม JPA 2.0 เข้าไป ถ้าใช้บน Websphere อาจจะต้องเอา javax.persistence ออกด้วยครับ ถ้ายังมี error อยู่
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
</exclusion>
</exclusions>
</dependency>
3. ต่อไปต้องเพิ่ม Code การโหลด entityManagerFacorry มาใหม่
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean getEntityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setDataSource(dataSource);
entityManagerFactory.setPackagesToScan("xxx.entities");
entityManagerFactory.setJpaVendorAdapter(getJPAVendor());
entityManagerFactory.setJpaProperties(getHibernateProperties());
return entityManagerFactory;
}
@Bean
public LocalSessionFactoryBean sessionFactory() throws IOException {
logger.debug("sessionFactory()");
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
factoryBean.setHibernateProperties(getHibernateProperties());
factoryBean.setDataSource(dataSource);
factoryBean.setPackagesToScan("xxx.entities");
return factoryBean;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
private JpaVendorAdapter getJPAVendor() {
HibernateJpaVendorAdapter hibernateJpaVendorAdaptor = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdaptor.setShowSql(true);
return hibernateJpaVendorAdaptor;
}
private Properties getHibernateProperties() {
Properties properties = new Properties();
// This is to get around a Websphere Bug
properties.put("hibernate.dialect", Oracle10gDialect.class.getName());
properties.put("hibernate.show_sql", "true");
logger.debug("properties : {}", properties);
return properties;
}