Spring3 and JPA Integration(I)

Spring3 and JPA Integration(I)

JPA POJO just stay the same com.sillycat.easyjpa.model.

We have the same interface for DAO layer com.sillycat.easyjpa.dao.PersonDAO:
package com.sillycat.easyjpa.dao;
import java.util.List;
import com.sillycat.easyjpa.model.Person;
public interface PersonDAO
{
    public void insert(Person person);
    public void updateName(String name, Integer id);
    public void update(Person person);
    public void delete(Integer id);
    public Person get(Integer id);
    public List<Person> getAll();
}

The implementation of the interface changes, we use jpaTempate to deal with entitymanager
com.sillycat.easyjpa.dao.PersonDAOImpl.java:
package com.sillycat.easyjpa.dao;
import java.util.List;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import com.sillycat.easyjpa.model.Person;
public class PersonDAOImpl extends JpaDaoSupport implements PersonDAO
{
    public void delete(Integer id)
    {
        Person person = getJpaTemplate().find(Person.class, id);
        if (person != null)
        {
            getJpaTemplate().remove(person);
        }
    }
    public Person get(Integer id)
    {
        return getJpaTemplate().find(Person.class, id);
    }
    @SuppressWarnings("unchecked")
    public List<Person> getAll()
    {
        List<Person> list = getJpaTemplate().find("select o from Person o order by o.id asc");
        return list;
    }
    public void insert(Person person)
    {
        getJpaTemplate().persist(person);
    }
    public void update(Person person)
    {
        getJpaTemplate().merge(person);
    }
    public void updateName(String name, Integer id)
    {
        Person person = getJpaTemplate().find(Person.class, id);
        if (person != null)
        {
            person.setName(name);
        }
    }
}

We have not start transaction in this layer, so we can not insert/update data in my unit test
com.sillycat.easyjpa.dao.PersonDAOTest.java:
package com.sillycat.easyjpa.dao;
import java.util.List;
import com.sillycat.core.BaseTestCase;
import com.sillycat.easyjpa.model.Person;
public class PersonDAOTest extends BaseTestCase
{
    private PersonDAO dao;
    public void setUp() throws Exception
    {
        super.setUp();
        dao = (PersonDAO) ctx.getBean("personDAO");
    }
    public void tearDown() throws Exception
    {
        super.tearDown();
    }
    public void testGetAll()
    {
        List<Person> list = dao.getAll();
        assertNotNull(list);
        assertTrue(list.size() > 0);
    }
    public void testGet()
    {
        Person t = dao.get(Integer.valueOf(1));
        assertNotNull(t);
    }
}

And this is the datasource configuration in resource-context.xml:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${dspool.maxactive}" />
<property name="maxIdle" value="${dspool.maxidle}" />
<property name="minIdle" value="1" />
<property name="maxWait" value="30000" />
<property name="defaultAutoCommit" value="true" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="300" />
<property name="logAbandoned" value="true" />
</bean>
<!-- entity manager factory,provided by spring-->
    <bean id="entityManagerFactory"
       class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
       <property name="dataSource" ref="dataSource" />
       <property name="persistenceXmlLocation"
           value="classpath:META-INF/persistence.xml" />
       <property name="loadTimeWeaver">
           <bean
               class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
       </property>
    </bean>
</beans>

my persistence.xml file is placed conf/META-INF/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="demoTest" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
</persistence-unit>
</persistence>

spring configuration file is dao-context.xml:
<bean id="personDAO" class="com.sillycat.easyjpa.dao.PersonDAOImpl" >
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

my sample prPuO[POST /admin/blogs HTTP/1.1
Accept: image/jpeg, application/x-ms-application, image/

你可能感兴趣的:(DAO,spring,xml,Hibernate,jpa)