[英]Spring+JPA/Hibernate questions
我有使用 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 client) {
clientDao.addClient(client);
}
}
下面是我的 DAO 類。
//@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>
應用上下文.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>
資源文件
<?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 Framework 事務管理? 有沒有其他選擇?
我發現了一些像這樣的例子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) 當我使用 EJB 時,我有容器管理的事務。 誰應該使用 Spring 管理事務?
答:Spring 也提供容器管理的事務支持(請參閱 JTA 事務管理器,示例 oracle 文章)和應用程序管理的事務(這意味着您的應用程序可以通過使用 Spring 事務 API 以最少的努力以聲明/編程方式管理事務)。 請參閱 Spring 事務文檔
2) 我是否一定需要使用 Spring Framework 事務管理? 有沒有其他選擇?
答:如果 spring 框架不管理您的事務,那么您的容器將需要管理它們。在同一點 1 spring 文檔中使用它們。 您甚至可以實現自己的事務管理器。
但是,如果您已經在使用 Spring 框架,那么為什么不從具有大量配置的 Spring 事務管理中受益呢,(Spring + Hibernate 事務管理器、Spring + JPAContainerManager、Spring+ JTAContainerManager 等...)
3) 我需要編輯 Java 代碼還是我的步驟應該在 xml 配置中?
答:您的事務管理器 xml 配置似乎可以使用 JpaTransactionManager,現在您可以通過注釋 @Transactional 在您的服務層 java 代碼中啟動事務,@Transactional 通常根據配置的事務管理器處理您的服務方法以參與事務。
@Service
@org.springframework.transaction.annotation.Transactional
public class Manager {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.