I've got JPA Toplink working well. But to wrok, I had to use the JpaDaoSupport.getJpaTemplate().
I'm a beginner, so take the following code has a working example, not a good example ;-)
Here are my files (sorry but XML file attachment is not allowed).
persistence.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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">
<persistence-unit name="Tap03PU" transaction-type="RESOURCE_LOCAL">
<provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
<class>tap03.domain.model.Document</class>
<class>tap03.domain.model.Configuration</class>
<properties>
<property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/tap03"/>
<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="toplink.jdbc.user" value="root"/>
<property name="toplink.jdbc.password" value=""/>
<property name="toplink.cache.type.default" value="NONE" />
<property name="toplink.logging.level" value="FINEST" />
<property name="toplink.logging.thread" value="true" />
<property name="toplink.logging.session" value="true" />
<property name="toplink.logging.exceptions" value="true" />
</properties>
</persistence-unit>
</persistence>
applicationContext.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="documentsManager" class="tap03.service.impl.DocumentsManagerImpl">
<property name="documentDao">
<ref bean="documentDAO" />
</property>
<property name="configurationManager">
<ref bean="configurationManager" />
</property>
</bean>
<bean id="configurationManager" class="tap03.service.impl.ConfigurationManagerImpl">
<property name="configurationDao">
<ref bean="configurationDAO" />
</property>
</bean>
</beans>
applicationContewxt-JPA.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="Tap03PU" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="oracle.toplink.essentials.platform.database.MySQL4Platform" />
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="documentDAOTarget" class="tap03.domain.dao.jpa.GenericDaoJpa" >
<constructor-arg>
<value>tap03.domain.model.Document</value>
</constructor-arg>
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="documentDAO" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
<property name="transactionManager" ref="transactionManager" />
<property name="target" ref="documentDAOTarget" />
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="configurationDAO" class="tap03.domain.dao.jpa.GenericDaoJpa" >
<constructor-arg>
<value>tap03.domain.model.Configuration</value>
</constructor-arg>
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
and finally the GenericDaoJpa.java:
Code:
package tap03.domain.dao.jpa;
import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import tap03.domain.dao.GenericDao;
public class GenericDaoJpa<T, ID extends Serializable>
extends JpaDaoSupport
implements GenericDao<T, ID>
{
private Class<T> entityClass;
public GenericDaoJpa()
{
}
public GenericDaoJpa(Class<T> type)
{
this.entityClass = type;
}
public Class<T> getPersistentClass()
{
return entityClass;
}
public T findById(ID id) throws ObjectRetrievalFailureException
{
return (T) getJpaTemplate().find(getPersistentClass(), id);
}
@SuppressWarnings("unchecked")
public Collection<T> findAll()
{
String q = "SELECT z FROM "+ this.entityClass.getSimpleName() +" z" ;
System.out.println( "GenericDaoJpa.findAll() q=\""+q+"\"" );
return (Collection<T>) this.getJpaTemplate().find(q);
}
@SuppressWarnings("unchecked")
public Collection<T> findByNamedQueryAndNamedParam(Class<T> entityClass,
String queryName, Map<String,Object> params ) throws DataAccessException
{
return (Collection<T>) this.getJpaTemplate().findByNamedQueryAndNamedParams( queryName, params);
}
public T saveOrUpdate(T entity)
{
return getJpaTemplate().merge(entity);
}
public void delete(ID id)
{
/*
* Entity must be managed to call remove: tap03.domain.model.Document[docId=11],
* try merging the detached and try the remove again.
*/
this.delete( this.findById(id) );
}
public void delete(T entity)
{
getJpaTemplate().remove(entity);
}
}