Java 如何在没有persistence.xml的情况下配置Spring?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21381943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure Spring without persistence.xml?
提问by membersound
I'm trying to set up spring xml configuration without having to create a futher persistence.xml
. But I'm constantly getting the following exception, even though I included the database properties in the spring.xml
我正在尝试设置 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:
弹簧.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>
What am I missing here?
我在这里缺少什么?
采纳答案by Zaki
Specify the "packagesToScan" & "persistenceUnitName" properties in the entityManagerFactory bean definition.
在 entityManagerFactory bean 定义中指定“packagesToScan”和“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>
Note that this is for Spring version > 3.1
请注意,这是针对 Spring 版本 > 3.1
回答by MariuszS
From Spring Guide Accessing Data with JPA
来自 Spring Guide使用 JPA 访问数据
@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
弹簧靴
With Spring Boot enabled application this is even easier:
使用启用 Spring Boot 的应用程序,这更容易:
Sample application.yaml
样本 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
回答by Mustafa
MariuszS' answer is good except that the entityManagerFactory
method should return EntityManagerFactory. To do that it can be written like this:
MariuszS 的回答很好,只是该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();
}
For future audience: below code worked:
对于未来的观众:以下代码有效:
@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();
}
回答by Vlad Mihalcea
Assuming that you have a PersistenceProvider
implementation (e.g. org.hibernate.jpa.HibernatePersistenceProvider
), you can use the PersistenceProvider#createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map)method to bootstrap an EntityManagerFactory
without needing a persistence.xml
.
假设您有一个PersistenceProvider
实现(例如org.hibernate.jpa.HibernatePersistenceProvider
),您可以使用PersistenceProvider#createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map)方法来引导 anEntityManagerFactory
而不需要persistence.xml
.
However, it's annoying that you have to implement the PersistenceUnitInfo
interface, so you are better off using Spring or Hibernate which both support bootstrapping JPA without a persistence.xml
file:
但是,您必须实现PersistenceUnitInfo
接口很烦人,因此最好使用 Spring 或 Hibernate,它们都支持在没有persistence.xml
文件的情况下引导 JPA :
this.nativeEntityManagerFactory = provider.createContainerEntityManagerFactory(
this.persistenceUnitInfo,
getJpaPropertyMap()
);
Where the PersistenceUnitInfois implemented by the Spring-specific MutablePersistenceUnitInfoclass.
凡PersistenceUnitInfo是由Spring特定的实施MutablePersistenceUnitInfo类。
Check out this articlefor a nice demonstration of how you can achieve this goal with Hibernate.
查看这篇文章,以很好地演示如何使用 Hibernate 实现这一目标。