Spring JPA Integration

来源:互联网 发布:mysql配置文件怎么写 编辑:程序博客网 时间:2024/06/05 03:24

1. persistence.xml

<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_1_0.xsd"             version="1.0">    <persistence-unit name="paranoid">        <provider>org.hibernate.ejb.HibernatePersistence</provider>        <properties>            <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />            <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:XE" />            <property name="hibernate.connection.username" value="paranoid" />            <property name="hibernate.connection.password" value="compelling" />            <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>            <property name="hibernate.hbm2ddl.auto" value="update"/>            <property name="hibernate.archive.autodetection" value="class"/>            <property name="hibernate.show_sql" value="true"/>            <property name="hibernate.format_sql" value="true"/>        </properties>    </persistence-unit></persistence>

2. applicationContext.xml

Note that no datasource referenced directly, the persistenceUnitManager is totally elegant!

<?xml version="1.0" encoding="UTF-8"?><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"xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/jdbchttp://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-3.0.xsd"><bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"><property name="entityManagerFactory" ref="emf" /></bean><tx:annotation-driven transaction-manager="transactionManager" /><bean id="persistenceUnitManager"class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"><property name="persistenceXmlLocation"><value>classpath*:META-INF/persistence.xml</value></property></bean><bean id="emf"class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"><property name="persistenceUnitManager" ref="persistenceUnitManager" /><property name="packagesToScan" value="com.derek.struts2.simple.domain" /></bean><context:annotation-config /><context:component-scan base-package="com.derek.struts2.simple" /><bean id="supplierHeaderServiceImpl"class="com.derek.struts2.simple.service.impl.SupplierHeaderServiceImpl"></bean></beans>

3. Service Implementation Annotation

@Service("supplierHeaderService")@Repository@Transactionalpublic class SupplierHeaderServiceImpl implements SupplierHeaderService {/* THINK: NO DAO LAYER NEEDED */@PersistenceContextprivate EntityManager session;/* private EntityTransaction tx; */public void setSession(EntityManager session) {this.session = session;}// Experiment Methodpublic List<SupplierHeader> findSupplierHeaderList() {// How to set parameters for finding all// Try out the lock mode parameter laterreturn (List<SupplierHeader>) session.find(SupplierHeader.class, "");}// Be aware of the difference with no transaction, readonly has consistent result during a complicated long report query@Transactional(readOnly = true)public List<SupplierHeader> findAll() {Query query = session.createQuery("SELECT e FROM SupplierHeader e");return (List<SupplierHeader>) query.getResultList();}// Transaction Scenario: CREATE OR UPDATEpublic SupplierHeader save(SupplierHeader sh) {if (sh.getEmail() == null) {session.persist(sh);} else {session.merge(sh);}return sh;}}





原创粉丝点击