Spring JPA和persistence.xml
我正在尝试设置一个Spring JPA Hibernate简单示例WAR来部署到Glassfish。 我看到一些示例使用persistence.xml文件,而其他示例则没有。 一些示例使用dataSource,而另一些示例则不使用。 到目前为止,我的理解是,如果我有以下情况,则不需要dataSource:
org.hibernate.ejb.HibernatePersistence com.coe.jpa.StudentProfile
我可以部署好,但是我的EntityManager没有被Spring注入。
我的applicationContext.xml:
我的EntityManager类:
public class StudentService { private String saveMessage; private String showModal; private String modalHeader; private StudentProfile studentProfile; private String lastName; private String firstName; @PersistenceContext(unitName="educationPU") private EntityManager em; @Transactional public String save() { System.out.println("*** em: " + this.em); //em is null this.studentProfile= new StudentProfile(); this.saveMessage = "saved"; this.showModal = "true"; this.modalHeader= "Information Saved"; return "successs"; }
我的web.xml:
org.springframework.web.context.ContextLoaderListener
是否有任何我错过的部分让Spring将“em”注入StudentService?
只是为了确认你可能做了……
你有没有包括
应用程序context.xml中的位?
另外我不太确定你能用这种设置的jta交易类型吗? 这不需要数据源托管连接池吗? 所以请尝试使用RESOURCE_LOCAL。
我很困惑。 您是将PU注入服务层而不是持久层? 我不明白。
我将持久层注入服务层。 服务层包含业务逻辑并划分事务边界。 它可以在事务中包含多个DAO。
我也没有在你的save()方法中获得魔力。 如何保存数据?
在生产中我配置像这样的弹簧:
以及web.xml中的引用
对于unit testing,我这样做:
如果有人想使用纯 Java配置而不是hibernate的xml
配置,请使用:
您可以在Spring中配置Hibernate而不使用persistence.xml,如下所示:
@Bean public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() { Map properties = new Hashtable<>(); properties.put("javax.persistence.schema-generation.database.action", "none"); HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter(); adapter.setDatabasePlatform("org.hibernate.dialect.MySQL5InnoDBDialect"); //you can change this if you have a different DB LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setJpaVendorAdapter(adapter); factory.setDataSource(this.springJpaDataSource()); factory.setPackagesToScan("package name"); factory.setSharedCacheMode(SharedCacheMode.ENABLE_SELECTIVE); factory.setValidationMode(ValidationMode.NONE); factory.setJpaPropertyMap(properties); return factory; }
由于您没有使用persistence.xml,因此您应该创建一个返回DataSource的bean,您可以在上面设置数据源的方法中指定它:
@Bean public DataSource springJpaDataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setUrl("jdbc:mysql://localhost/SpringJpa"); dataSource.setUsername("tomcatUser"); dataSource.setPassword("password1234"); return dataSource; }
然后在此配置文件上使用@EnableTransactionManagement
批注。 现在当您放置该注释时,您必须创建一个最后一个bean:
@Bean public PlatformTransactionManager jpaTransactionManager() { return new JpaTransactionManager( this.entityManagerFactoryBean().getObject()); }
现在,不要忘记对那些处理DB的方法使用@Transactional
Annotation。
最后,不要忘记在您的存储库中注入EntityManager
(此存储库类应该对其进行@Repository
注释)。
我有一个使用JPA / Hibernate和Spring设置的测试应用程序,我的配置镜像你的,除了我创建一个数据源并将其注入EntityManagerFactory,并将数据源特定属性移出persistenceUnit并移入数据源。 通过这两个小变化,我的EM被正确注入。
这可能是旧的,但如果有人遇到同样的问题,请尝试将unitname更改为PersistenceContext注释中的name:
从
@PersistenceContext(unitName="educationPU")
至
@PersistenceContext(name="educationPU")