Friday, 13 September 2013

Spring 3.2.4, EclipseLink 2.5, Glassfish 3.1.2 and JPA 2.1

Spring 3.2.4, EclipseLink 2.5, Glassfish 3.1.2 and JPA 2.1

I'm very new in the world of Spring and Glassfish. I've been fighting with
the configuration of the one application using these topics, but I don't
get create the application. Anybody can help me please?
I want to create an application with one persistence.xml, jndi data
source, generic dao configuration.
Thanks for your help!
Here is my code, but I think that there are some conflicts with the
configuration.
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="spartanPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/__spartanDS</jta-data-source>
<class>com.spartan.model.entities.SourceEntity</class>
<properties>
<property name="eclipselink.logging.level.sql" value="FINE" />
<property name="eclipselink.logging.parameters" value="true" />
<property name="eclipselink.logging.level" value="FINE" />
<property name="javax.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver" />
<property name="eclipselink.ddl-generation" value="none" />
<property name="eclipselink.target-database" value="MySQL" />
<property name="eclipselink.jdbc.native-sql" value="true" />
<property name="eclipselink.jdbc.cache-statements" value="true" />
</properties>
</persistence-unit>
dao-bean.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
default-lazy-init="true" default-autowire="no">
<tx:annotation-driven />
<bean id="sourceDao" class="com.spartan.model.daos.SourceDao">
<property name="entityClass" >
<bean class="com.spartan.model.entities.SourceEntity" />
</property>
</bean>
application-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:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
default-autowire="no">
<tx:annotation-driven transaction-manager="transactionManager" />
<context:annotation-config />
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/__spartanDS" />
<import resource="classpath:com/spartan/model/spring/dao-beans.xml" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation"
value="classpath:META-INF/persistence.xml" />
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="spartanPU" />
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
<property name="databasePlatform"
value="org.hibernate.dialect.MySQLDialect" />
</bean>
</property>
<property name="jpaDialect">
<bean
class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect"/>
</property>
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"/>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"
/>
<!-- Exception translation bean post processor -->
<bean
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"
/>
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
/>
<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
BaseDao.java
import java.lang.reflect.ParameterizedType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
@Transactional
public abstract class BaseDao<PK, T> implements IBaseDao<PK, T> {
protected T entityClass;
@PersistenceContext
protected EntityManager entityManager;
@SuppressWarnings({ "unchecked" })
public BaseDao() {
ParameterizedType genericSuperclass = (ParameterizedType) getClass()
.getGenericSuperclass();
this.entityClass = ((T) genericSuperclass.getActualTypeArguments()[0]);
}
@Transactional
public T create(T entity) {
this.entityManager.persist(entity);
this.entityManager.flush();
return entity;
}
public T read(PK id) {
return null;
}
public T update(T entity) {
this.entityManager.merge(entity);
this.entityManager.flush();
return entity;
}
public void delete(T entity) {
this.entityManager.remove(entity);
}
public void setEntityClass(T entityClass) {
this.entityClass = entityClass;
}
}

No comments:

Post a Comment