Java 弹簧&x2B;JPA/Hibernate问题
我有使用EJB+JPA+Hibernate将实体保存到DB的工作代码。现在我需要将EJB更改为Spring 下面是我的简化经理课程Java 弹簧&x2B;JPA/Hibernate问题,java,spring,hibernate,jpa,ejb,Java,Spring,Hibernate,Jpa,Ejb,我有使用EJB+JPA+Hibernate将实体保存到DB的工作代码。现在我需要将EJB更改为Spring 下面是我的简化经理课程 //@Stateless - changed to @Service @Service public class Manager { //@EJB - changed to Autowired @Autowired private ClientDao clientDao; public void addClient(Client
//@Stateless - changed to @Service
@Service
public class Manager {
//@EJB - changed to Autowired
@Autowired
private ClientDao clientDao;
public void addClient(Client client) {
clientDao.addClient(client);
}
}
下面是我的刀课
//@Stateless - changed to @Repository
@Repository
public class JpaClientDao implements ClientDao {
@PersistenceContext(unitName="ClientsService")
private EntityManager em;
public void addClient(Client client) {
em.persist(client);
}
}
下面是persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="ClientsService" transaction-type = "JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>myJtaDatabase</jta-data-source>
<class>com.entity.Client</class>
<properties>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
org.hibernate.jpa.HibernatePersistenceProvider
myJtaDatabase
com.entity.Client
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com" />
<context:annotation-config/>
</beans>
resources.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<Resource id="myJtaDatabase" type="DataSource">
JdbcDriver org.apache.derby.jdbc.ClientDriver
JdbcUrl jdbc:derby://localhost:1527/C:/ClientDB
UserName test
Password 123456
validationQuery = SELECT 1
JtaManaged true
</Resource>
</resources>
JdbcDriver org.apache.derby.jdbc.ClientDriver
JdbcUrl jdbc:derby://localhost:1527/C:/ClientDB
用户名测试
密码123456
validationQuery=选择1
真的吗
问题1) 当我使用EJB时,我有容器管理的事务。谁应该使用Spring管理事务
2) 我是否需要使用Spring框架事务管理?还有其他选择吗
我找到了一些这样的例子,我无法确定是spring特定的代码还是适合我的代码
<!-- ************ JPA configuration *********** -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:config/persistence-demo.xml" />
<property name="dataSource" ref="restDemoDS" />
<property name="packagesToScan" value="org.codingpedia.demo.*" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
</bean>
</property>
</bean>
3) 我是否需要编辑Java代码,或者我的步骤应该是xml配置
鼓励任何有用的链接
1) 当我使用EJB时,我有容器管理的事务。谁应该
使用Spring管理事务
答:Spring还提供容器管理的事务支持(请参阅JTA事务管理器,oracle示例)和应用程序管理的事务(这意味着您的应用程序可以使用Spring事务API以声明/编程的方式管理事务)
2) 我是否需要使用Spring框架事务
管理层?还有其他选择吗
答:如果spring framework没有管理您的事务,那么您的容器将需要管理它们。您可以选择任何Java EE JTA实现,如开源JBossTS或企业JTA实现Oracle WebLogicJtaTransactionManager或IBM WebSphereUowTransactionManager,您可以在相同的Point1Spring文档中找到如何使用它们。您甚至可以实现自己的事务管理器
但是,如果您已经在使用Spring框架,那么为什么不利用Spring事务管理和大量可能的配置(Spring+Hibernate事务管理器、Spring+JPA容器管理器、Spring+JTA容器管理器等…)
3) 我是否需要编辑Java代码,或者我的步骤应该是xml
配置
答:您的transaction manager xml配置似乎可以使用JpaTransactionManager,现在您可以通过注释@Transactional在服务层java代码中启动事务,该注释通常处理您的服务方法,以根据配置的事务管理器参与事务
@Service
@org.springframework.transaction.annotation.Transactional
public class Manager {
在EJBCMT代码中,您之前在哪里声明事务管理?