Spring 创建名为';实体管理工厂&x27;在类路径资源中定义
我正在尝试将xml应用程序配置文件转换为基于Java的配置。 我遇到了实体管理器的问题。它与persistence.xml和web.xml配合得很好Spring 创建名为';实体管理工厂&x27;在类路径资源中定义,spring,jpa,entitymanager,Spring,Jpa,Entitymanager,我正在尝试将xml应用程序配置文件转换为基于Java的配置。 我遇到了实体管理器的问题。它与persistence.xml和web.xml配合得很好 <!-- Entity Manager --> <jee:jndi-lookup id="entityManagerFactory" jndi-name="jdbc/derby"></jee:jndi-lookup> PersistenceContext.java package com.marc.embedd
<!-- Entity Manager -->
<jee:jndi-lookup id="entityManagerFactory" jndi-name="jdbc/derby"></jee:jndi-lookup>
PersistenceContext.java
package com.marc.embeddedglassfish.config;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.support.AbstractPlatformTransactionManager;
import com.marc.springmvc3.dao.PersonDAO;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"com.marc.springmvc3.repositories"})
public class PersistenceContext {
/* glassfish requires "java:app/"
Deployment Descriptor. An application-scoped resource is defined in the glassfish-resources.xml deployment descriptor file.
This file is placed in the META-INF directory of the module or application archive. For web applications or modules,
this file is placed in the WEB-INF directory. If any submodule archives of an enterprise application archive have their
own glassfish-resources.xml files, the resource definitions are scoped to those modules only. For more information about
the glassfish-resources.xml file, see Appendix B, GlassFish Server Deployment Descriptor Files and Appendix C, Elements of the
GlassFish Server Deployment Descriptors.
Naming. Application-scoped resource JNDI names begin with java:app or java:module. If one of these prefixes is not specified
in the JNDI name, it is added. For example, application-scoped databases have JNDI names in the following format:
java:app/jdbc/DataSourceName or java:module/jdbc/DataSourceName. This is in accordance with the naming scopes introduced
in the Java EE 6 Specification.
http://docs.oracle.com/cd/E18930_01/html/821-2417/giydj.html
oracle glassfish application deployment guide
*/
@Bean
public DataSource dataSource() {
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(true);
DataSource dataSource = dsLookup.getDataSource("java:app/jdbc/TestDB");
return dataSource;
}
@Bean
public PersonDAO personDao() {
PersonDAO personDao = new PersonDAO();
personDao.setDataSource(dataSource());
return personDao;
}
@Bean
public AbstractPlatformTransactionManager transactionManager() {
return new JpaTransactionManager(entityManagerFactory());
}
@Bean
public DataSource dataSourceForEntityManager() {
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(true);
// "jdbc/__TimerPool" is defined by default in embedded glassfish
DataSource dataSource = dsLookup.getDataSource("jdbc/__TimerPool");
return dataSource;
}
@Bean
public EntityManagerFactory entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
EclipseLinkJpaVendorAdapter vendor = new EclipseLinkJpaVendorAdapter();
vendor.setDatabase(Database.DERBY);
vendor.setGenerateDdl(true);
vendor.setShowSql(true);
entityManagerFactoryBean.setJpaVendorAdapter(vendor);
entityManagerFactoryBean.setJtaDataSource(dataSourceForEntityManager());
entityManagerFactoryBean.setPackagesToScan(new String[] { "com.marc.springmvc3.model" });
final Properties props = new Properties();
props.setProperty("javax.persistence.jdbc.driver", "org.apache.derby.jdbc.EmbeddedDriver");
props.setProperty("eclipselink.target-database", "DERBY");
props.setProperty("eclipselink.ddl-generation", "drop-and-create-table");
props.setProperty("eclipselink.logging.level.sql", "FINEST");
props.setProperty("eclipselink.logging.parameters", "true");
entityManagerFactoryBean.setJpaProperties(props);
entityManagerFactoryBean.afterPropertiesSet();
return entityManagerFactoryBean.getObject();
}
}
package com.marc.springmvc3.service;
import java.util.List;
import javax.inject.Inject;
import org.springframework.stereotype.Service;
import com.marc.springmvc3.model.Person;
import com.marc.springmvc3.repositories.PersonRepository;
@Service
public class PersonService {
@Inject
private PersonRepository repository;
@Inject
private List<Person> persons;
public List<Person> getAllPersons() {
Person person = new Person();
person.setFirstName("Lucky");
person.setName("Luke");
repository.save(person);
person.setFirstName("");
person.setName("");
Person dbPerson = repository.findOne(person.getId());
persons.add(dbPerson);
return persons;
}
}
package com.marc.springmvc3.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import com.marc.springmvc3.model.Person;
public interface PersonRepository extends JpaRepository<Person, Integer> {
}
PersonService.java
package com.marc.embeddedglassfish.config;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.support.AbstractPlatformTransactionManager;
import com.marc.springmvc3.dao.PersonDAO;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"com.marc.springmvc3.repositories"})
public class PersistenceContext {
/* glassfish requires "java:app/"
Deployment Descriptor. An application-scoped resource is defined in the glassfish-resources.xml deployment descriptor file.
This file is placed in the META-INF directory of the module or application archive. For web applications or modules,
this file is placed in the WEB-INF directory. If any submodule archives of an enterprise application archive have their
own glassfish-resources.xml files, the resource definitions are scoped to those modules only. For more information about
the glassfish-resources.xml file, see Appendix B, GlassFish Server Deployment Descriptor Files and Appendix C, Elements of the
GlassFish Server Deployment Descriptors.
Naming. Application-scoped resource JNDI names begin with java:app or java:module. If one of these prefixes is not specified
in the JNDI name, it is added. For example, application-scoped databases have JNDI names in the following format:
java:app/jdbc/DataSourceName or java:module/jdbc/DataSourceName. This is in accordance with the naming scopes introduced
in the Java EE 6 Specification.
http://docs.oracle.com/cd/E18930_01/html/821-2417/giydj.html
oracle glassfish application deployment guide
*/
@Bean
public DataSource dataSource() {
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(true);
DataSource dataSource = dsLookup.getDataSource("java:app/jdbc/TestDB");
return dataSource;
}
@Bean
public PersonDAO personDao() {
PersonDAO personDao = new PersonDAO();
personDao.setDataSource(dataSource());
return personDao;
}
@Bean
public AbstractPlatformTransactionManager transactionManager() {
return new JpaTransactionManager(entityManagerFactory());
}
@Bean
public DataSource dataSourceForEntityManager() {
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(true);
// "jdbc/__TimerPool" is defined by default in embedded glassfish
DataSource dataSource = dsLookup.getDataSource("jdbc/__TimerPool");
return dataSource;
}
@Bean
public EntityManagerFactory entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
EclipseLinkJpaVendorAdapter vendor = new EclipseLinkJpaVendorAdapter();
vendor.setDatabase(Database.DERBY);
vendor.setGenerateDdl(true);
vendor.setShowSql(true);
entityManagerFactoryBean.setJpaVendorAdapter(vendor);
entityManagerFactoryBean.setJtaDataSource(dataSourceForEntityManager());
entityManagerFactoryBean.setPackagesToScan(new String[] { "com.marc.springmvc3.model" });
final Properties props = new Properties();
props.setProperty("javax.persistence.jdbc.driver", "org.apache.derby.jdbc.EmbeddedDriver");
props.setProperty("eclipselink.target-database", "DERBY");
props.setProperty("eclipselink.ddl-generation", "drop-and-create-table");
props.setProperty("eclipselink.logging.level.sql", "FINEST");
props.setProperty("eclipselink.logging.parameters", "true");
entityManagerFactoryBean.setJpaProperties(props);
entityManagerFactoryBean.afterPropertiesSet();
return entityManagerFactoryBean.getObject();
}
}
package com.marc.springmvc3.service;
import java.util.List;
import javax.inject.Inject;
import org.springframework.stereotype.Service;
import com.marc.springmvc3.model.Person;
import com.marc.springmvc3.repositories.PersonRepository;
@Service
public class PersonService {
@Inject
private PersonRepository repository;
@Inject
private List<Person> persons;
public List<Person> getAllPersons() {
Person person = new Person();
person.setFirstName("Lucky");
person.setName("Luke");
repository.save(person);
person.setFirstName("");
person.setName("");
Person dbPerson = repository.findOne(person.getId());
persons.add(dbPerson);
return persons;
}
}
package com.marc.springmvc3.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import com.marc.springmvc3.model.Person;
public interface PersonRepository extends JpaRepository<Person, Integer> {
}
应该是:
@Bean
public FactoryBean<EntityManagerFactory> entityManagerFactory() {
...
entityManagerFactoryBean.setJpaProperties(props);
return entityManagerFactoryBean;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
@Bean
public FactoryBean EntityManager工厂(){
...
setJpaProperties(props);
返回entityManagerFactoryBean;
}
@豆子
公共平台transactionManager transactionManager(EntityManager工厂EntityManager工厂){
返回新的JpaTransactionManager(entityManagerFactory);
}
不建议直接从代码实例化任何FactoryBean
:只需要依赖Springcontaier
要使用FactoryBean
结果,必须遵循参数injection
策略。它应该是:
@Bean
public FactoryBean<EntityManagerFactory> entityManagerFactory() {
...
entityManagerFactoryBean.setJpaProperties(props);
return entityManagerFactoryBean;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
@Bean
public FactoryBean EntityManager工厂(){
...
setJpaProperties(props);
返回entityManagerFactoryBean;
}
@豆子
公共平台transactionManager transactionManager(EntityManager工厂EntityManager工厂){
返回新的JpaTransactionManager(entityManagerFactory);
}
不建议直接从代码实例化任何FactoryBean
:只需要依赖Springcontaier
要使用FactoryBean
结果,必须遵循参数injection
策略。我解决了它
堆栈跟踪的有趣部分位于末尾:
原因:java.lang.IllegalStateException:在未指定LoadTimeWeaver的情况下无法应用类转换器
这里的解释帮助了我:
我仍然不知道如何在基于Java的应用程序上下文配置中从glassfish获得EntitManagerFactory作为JNDI。有什么想法吗?我解决了
堆栈跟踪的有趣部分位于末尾:
原因:java.lang.IllegalStateException:在未指定LoadTimeWeaver的情况下无法应用类转换器
这里的解释帮助了我:
我仍然不知道如何在基于Java的应用程序上下文配置中从glassfish获得EntitManagerFactory作为JNDI。有什么想法吗?谢谢你的回答。尽管如此,我还是不太明白。。。你能告诉我更多的细节吗?谢谢你的回答。尽管如此,我还是不太明白。。。你能告诉我更多的细节吗?