我正在尝试建立一个Spring JPA Hibernate简单的示例WAR,用于部署到Glassfish。我看到一些示例使用persistence.xml文件,而其他示例则不使用。有些示例使用dataSource,有些则不使用。到目前为止,我的理解是,如果我有以下条件,则不需要dataSource:
<persistence-unit name="educationPU"
transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.coe.jpa.StudentProfile</class>
<properties>
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/COE" />
<property name="hibernate.connection.username" value="root" />
<property name="show_sql" value="true" />
<property name="dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>我可以很好地部署,但是我的EntityManager没有被Spring注入。
我的applicationContext.xml:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="educationPU" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="StudentProfileDAO" class="com.coe.jpa.StudentProfileDAO">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="studentService" class="com.coe.services.StudentService">
</bean>我使用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:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
有没有什么东西是我遗漏的,无法让Spring将"em“注入到StudentService中?
发布于 2009-07-16 16:55:37
我只想确认一下,虽然你可能做了.
您是否包含了
<!-- tell spring to use annotation based congfigurations -->
<context:annotation-config />
<!-- tell spring where to find the beans -->
<context:component-scan base-package="zz.yy.abcd" />您的应用程序context.xml中的bits?
另外,我不确定您是否能够在这种设置中使用jta事务类型?这难道不需要一个数据源管理连接池吗?因此,请尝试使用RESOURCE_LOCAL。
发布于 2009-08-07 12:51:18
我很困惑。您将PU注入到服务层而不是持久层?我不明白。
我将持久层注入到服务层中。服务层包含业务逻辑并划分事务边界。它可以在一个事务中包含多个DAO。
我也不明白你的save()方法有什么神奇之处。数据是如何保存的?
在生产环境中,我这样配置spring:
<jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/ThePUname" />以及web.xml中的引用
对于单元测试,我这样做:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource" p:persistence-xml-location="classpath*:META-INF/test-persistence.xml"
p:persistence-unit-name="RealPUName" p:jpaDialect-ref="jpaDialect"
p:jpaVendorAdapter-ref="jpaVendorAdapter" p:loadTimeWeaver-ref="weaver">
</bean>发布于 2017-04-06 11:52:52
如果有人想要使用纯 Java配置,而不是hibernate的xml配置,请使用以下代码:
您可以在Spring中完全不使用persistence.xml来配置Hibernate,如下所示:
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean()
{
Map<String, Object> 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是在设置数据源的上述方法中指定的:
@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注释。
最后,不要忘记在存储库中注入EntityManager (这个存储库类应该有@Repository注释)。
https://stackoverflow.com/questions/1132565
复制相似问题