【发布时间】:2017-07-04 11:07:30
【问题描述】:
在springthis的官方文档中写下:
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.acme.domain");
factory.setDataSource(dataSource());
return factory;
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}
和
创建 LocalContainerEntityManagerFactoryBean 和 不是 EntityManagerFactory 直接因为前者也参与 在异常翻译机制中除了简单地创建 EntityManagerFactory。
但是当我尝试使用它时,我得到了错误:
setEntityManagerFactory
(javax.persistence.EntityManagerFactory)
in JpaTransactionManager cannot be applied
to
(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)
这是我的进口:
import org.apache.tomcat.jdbc.pool.DataSourceFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
【问题讨论】:
-
entityManagerFactory().getObject()。或者只是将其作为方法参数注入transactionManager(EntityManagerFactory emf) {...}。 -
我做到了 getNativeEntityManagerFactory .....tnx。你能看到我关于实体 ID 的另一个问题吗?
-
不要使用它。如前所述,使用 getObject instemde。
-
它解决了我的两个问题。两种方法都返回一个对象
-
您仍然不应该这样做,因为您正在绕过弹簧逻辑和回调,手头可能会出现奇怪的问题。
标签: java spring hibernate spring-data-jpa