Spring+JPA/Hibernate 个问题

Spring+JPA/Hibernate questions

我有使用 EJB+JPA+Hibernate 将实体保存到数据库的工作代码。现在我需要将 EJB 更改为 Spring.

下面是我的简化管理器class。

//@Stateless - changed to @Service
@Service
public class Manager {

    //@EJB - changed to Autowired
    @Autowired
    private ClientDao clientDao;

    public void addClient(Client client) {
        clientDao.addClient(client);
    }
}

下面是我的 DAO class。

//@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>

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>

resoures.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>

问题。
1) 当我使用 EJB 时,我有容器管理的事务。谁应该使用 Spring 来管理交易?
2) 我是否必须使用 Spring 框架事务管理?有没有其他选择?
我找到了一些像这样的例子 http://www.codingpedia.org/ama/java-persistence-example-with-spring-jpa2-and-hibernate/,我无法理解下面的代码 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) When I used EJB, I had Container-Managed Transactions. Who should manage transactions with using Spring?

答案:Spring 也提供容器管理的事务支持(参见 JTA 事务管理器,示例 oracle article) and also application managed transaction (which means your application can declaratively/programatically manage the transactions with minimal effort by using Spring transaction API ) . See Spring transaction doc

2) Do I need to necessarily use Spring Framework transaction management? Is there any alternatives?

回答:如果 spring 框架没有管理您的事务,那么您的容器将需要管理它们。您可以选择任何 Java EE JTA 实现,例如开源 JBossTS 或企业 JTA 实现Oracle-WebLogicJtaTransactionManager 或 IBM-WebSphereUowTransactionManager,您可以在同一点 1 spring 文档中找到如何使用它们。您甚至可以实现自己的事务管理器。

但是,如果您已经在使用 Spring 框架,为什么不从 spring 具有大量配置的事务管理中获益,(Spring + Hibernate 事务管理器,Spring + JPAContainerManager,Spring+ JTAContainerManager 等)

3) Do I need to edit Java code or should my steps be in xml configuration?

回答:您的交易管理器 xml 配置似乎可以使用 JpaTransactionManager,现在您可以通过注释 @Transactional 在您的服务层 java 代码中启动交易,它通常处理您的服务方法以参与根据配置的事务管理器在事务中。

@Service
@org.springframework.transaction.annotation.Transactional
public class Manager {