我正在尝试设置spring xml配置,而不必创建更进一步的persistence.xml
。但是我经常遇到以下异常,即使我在spring.xml
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in file [C:\Users\me\workspace\app\src\main\webapp\WEB-INF\applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}
spring.xml:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
</props>
</property>
</bean>
我在这里缺少什么?
答案 0 :(得分:41)
@Configuration
@EnableJpaRepositories
public class Application {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(H2).build();
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
lef.setDataSource(dataSource);
lef.setJpaVendorAdapter(jpaVendorAdapter);
lef.setPackagesToScan("hello");
return lef;
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setShowSql(false);
hibernateJpaVendorAdapter.setGenerateDdl(true);
hibernateJpaVendorAdapter.setDatabase(Database.H2);
return hibernateJpaVendorAdapter;
}
启用Spring Boot应用程序后,这更容易:
示例application.yaml
spring:
datasource:
url: jdbc:h2:mem:test
username: sa
password: sa
driver-class-name: org.h2.Driver
jpa:
database: H2
show-sql: false
hibernate:
format_sql: true
ddl-auto: auto
答案 1 :(得分:28)
指定“packagesToScan”&amp; entityManagerFactory bean定义中的“persistenceUnitName”属性。
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="myPersistenceUnit" />
<property name="packagesToScan" >
<list>
<value>org.mypackage.*.model</value>
</list>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
</props>
</property>
</bean>
请注意,这是针对Spring版本&gt; 3.1
答案 2 :(得分:3)
MariuszS&#39;答案是好的,除了entityManagerFactory
方法应该返回EntityManagerFactory。要做到这一点,可以这样写:
@Bean
public EntityManagerFactory entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
lef.setDataSource(dataSource);
lef.setJpaVendorAdapter(jpaVendorAdapter);
lef.setPackagesToScan("hello");
return lef.object();
}
对于未来的受众:以下代码有效:
@Bean (name = "entityManagerFactory")
public EntityManagerFactory entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter)
{
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
lef.setDataSource(dataSource);
lef.setJpaVendorAdapter(jpaVendorAdapter);
lef.setPackagesToScan("*.models*");
lef.afterPropertiesSet(); // It will initialize EntityManagerFactory object otherwise below will return null
return lef.getObject();
}
答案 3 :(得分:2)
假设您有PersistenceProvider
实施(例如org.hibernate.jpa.HibernatePersistenceProvider
),您可以使用PersistenceProvider#createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map)方法引导EntityManagerFactory
而无需persistence.xml
。
然而,你必须实现PersistenceUnitInfo
接口很烦人,所以你最好使用Spring或Hibernate,它们都支持在没有persistence.xml
文件的情况下引导JPA:
this.nativeEntityManagerFactory = provider.createContainerEntityManagerFactory(
this.persistenceUnitInfo,
getJpaPropertyMap()
);
PersistenceUnitInfo类由特定于Spring的MutablePersistenceUnitInfo类实现。
查看this article,了解如何使用Hibernate实现这一目标。