spring eclipselink problem with IntegrityException - spring

I try to use spring with eclipseLink and I've got an IntegrityException.
This is my configuration:
<?xml version="1.0" encoding="UTF-8"?>
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<import resource="data-source.xml" />
<tx:annotation-driven mode="proxy"
transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory" />
<!-- Entity manager -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="unit1" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"/>
</property>
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
<property name="databasePlatform" value="org.eclipse.persistence.platform.database.DerbyPlatform" />
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
</bean>
<!-- <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> -->
<!-- <property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect"
/> -->
<!-- <property name="showSql" value="true" /> -->
<!-- <property name="generateDdl" value="true" /> -->
<!-- </bean> -->
</property>
</bean>
<bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
and exactly exception is:
[EL Config]: 2011-06-09 00:15:24.061--ServerSession(2050312009)--Connection(473155160)--Thread(Thread[main,5,main])--Connected: jdbc:derby://localhost:1527/springhib;create=true
User: app
Database: Apache Derby Version: 10.6.2.1 - (999685)
Driver: Apache Derby Network Client JDBC Driver Version: 10.7.1.1 - (1040133)
[EL Severe]: 2011-06-09 00:15:24.127--ServerSession(2050312009)--Thread(Thread[main,5,main])--Local Exception Stack:
Exception [EclipseLink-0] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions:
Exception [EclipseLink-148] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The container policy [CollectionContainerPolicy(class org.eclipse.persistence.indirection.IndirectSet)] is not compatible with transparent indirection.
Mapping: org.eclipse.persistence.mappings.ManyToManyMapping[comments]
Descriptor: RelationalDescriptor(pl.adaknet.hibspring.domain.ArtEntity --> [DatabaseTable(ARTENTITY)])
but i don't have this problem when I use another Vendor
org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter
any ideas?

Try to use this loadtimeweaver implementation:
package net.palesz.util;
import org.springframework.instrument.classloading.SimpleLoadTimeWeaver;
public class JpaAwareLoadTimeWeaver extends SimpleLoadTimeWeaver {
#Override
public ClassLoader getInstrumentableClassLoader() {
ClassLoader instrumentableClassLoader = super.getInstrumentableClassLoader();
if (instrumentableClassLoader.getClass().getName().endsWith("SimpleInstrumentableClassLoader")) {
return instrumentableClassLoader.getParent();
} else {
return instrumentableClassLoader;
}
}
}
Spring context.xml config:
<bean id="loadTimeWeaver" class="net.palesz.util.JpaAwareLoadTimeWeaver" />

An odd error because IndirectSet is valid. It seems to be a class loader issue, but I have not seen this in Spring before.
It could be related to your usage of loadTimeWeaver, so you could try removing that.
In what environment are you running Spring?

you have to use agent for dynamic weaving
-javaagent:spring-agent-2.5.6.jar
i had tried eclipseLink agent, but it didn't work.
more info about weaving for JPA on Spring:
http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch14s06.html
if you would like run your code on tomcat:
http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch14s06.html#orm-jpa-setup-lcemfb-tomcat

Related

Why is my local JPA setup not honored if using an EntityManagerFactory from JNDI using Spring?

AuditingEntityListener correctly updates columns marked with #LastModifiedDate, #CreatedDate, #CreatedBy and #LastModifiedBy in dev mode (mvn jetty:run) when I use LocalContainerEntityManagerFactoryBean. However, when I activate prod profile and deploy in Wildfly 8, the columns are not updated.
I found on this forum post: "You will need to use one of Spring's EntityManagerFactoryBeans to setup the EntityManagerFactory" Is there any way to use AuditingEntityListener with <jee:jndi-lookup /> EntityManagerFactory?
Here is my applicationContext.xml
<beans profile="dev">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceXmlLocation" value="classpath:/META-INF/local-container-persistence.xml" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
<beans profile="prod">
<jee:jndi-lookup id="dataSource" jndi-name="java:jboss/datasources/postgresql-datasource" lookup-on-startup="false" expected-type="javax.sql.DataSource" />
<jee:jndi-lookup id="entityManagerFactory" jndi-name="java:jboss/entity-manager-factory" lookup-on-startup="false" expected-type="javax.persistence.EntityManagerFactory" />
<tx:jta-transaction-manager />
</beans>
<jpa:repositories base-package="com.corp.repository" factory-class="com.corp.RespositoryFactoryBean" />
<jpa:auditing auditor-aware-ref="entityAuditorAware" />
<bean name="entityAuditorAware" class="com.corp.EntityAuditorAware" />
orm.xml
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
version="2.0">
<persistence-unit-metadata>
<persistence-unit-defaults>
<entity-listeners>
<entity-listener class="org.springframework.data.jpa.domain.support.AuditingEntityListener" />
</entity-listeners>
</persistence-unit-defaults>
</persistence-unit-metadata>
</entity-mappings>
tl;dr
Make sure the EntityManagerFactory you obtain from JNDI is configured to have the AuditingEntityListener applied to the persistence unit you obtain.
Details
In your dev profile, The EntityManagerFactory is created within your application and thus - by definition in the spec - considers the locally available orm.xml.
In you prod example you don't bootstrap an EntityManagerFactory locally but obtain a preconfigured one from your application server. Thus you have to make sure the EntityManagerFactory instance is configured as you expected in the application server in the first place.

build jar with maven, contain a list of spring service and add it to war

I am using Spring with Maven and Tomcat. I have a Spring MVC application which has a public front-end WAR,and a shared services JAR. My JAR contain list of service annotated by spring annotation #Service and i configure my Application context inside this jar like bellow.
<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:sws="http://www.springframework.org/schema/web-services"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/web-services
http://www.springframework.org/schema/web-services/web-services-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.bergit.jpa" />
<!-- Import file which containt parameter need it to configure the DB and hibernate -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="META-INF/config/jdbc.properties" />
<!-- Database connection settings -->
<bean id="dataSource-seconde"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<!-- Configuration for Hibernate/JPA -->
<bean id="entityManagerFactory-seconde"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- <property name="dataSource" ref="dataSource-seconde" /> -->
<property name="persistenceUnitManager" ref="pum"/>
<property name="persistenceUnitName" value="testspring-jpa-seconde" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="${jdbc.dialect}" />
</bean>
</property>
</bean>
<bean id="pum"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>META-INF/persistence.xml</value>
</list>
</property>
</bean>
<bean id="transactionManager-seconde"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory-seconde" />
<property name="dataSource" ref="dataSource-seconde" />
</bean>
</beans>
and this is my class java is writed like bellow:
#Service("userJARService")
public class UserJARServiceImpl implements UserJARService {...}
i tested my Jar using Junit and it's ok.
now i add this jar into my war (add inside the pom file of war).
i write my class of controller for my first view where i inject my first service which i want to use it inside this conctroller UserJARService like bellow:
#Controller
#RequestMapping(AccessClassToWebService.CONTROLLER_BASE_PATH)
public class AccessClassToWebService {
#Autowired
UserJARService userJARService;
}
now i add this jar dependency inside the pom of my war. But i get the error bellow
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.bergit.jpa.service.UserJARService ....

Setting up Spring JTA transaction in Jboss 7.1.1

Hello I am trying to setup spring JTA in myEclipse for Spring.Below are my configuration files:
applicationContent.xml where i have added two imports(note it do include schema locations)
<import resource="infrastructure.xml"/>
<import resource="classpath:**/persistence.xml"/>
infrastrutre.xml(unable to add schema due to input validations)
<bean
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jtaDataSource" ref="masterDataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>
<!-- <bean id="masterDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:/test_JTA"></property>
</bean> -->
<jee:jndi-lookup id="masterDataSource" jndi-name="java:/Test_JTA" />
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!-- <property name="databasePlatform"
value="org.hibernate.dialect.MySQL5InnoDBDialect" /> -->
<property name="database" value="MYSQL" />
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="mainPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.abc.PaymentCard</class>
<jar-file>mysql-connector-java-5.1.23-bin.jar</jar-file>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/>
<property key="hibernate.transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property>
<property key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
</properties>
</persistence-unit>
</persistence>
I am getting error
JBAS010402: Unable to instantiate driver class "com.mysql.jdbc.Driver": org.jboss.msc.service.DuplicateServiceException: Service jboss.jdbc-driver.JTA_Test_war is already registered
I goggled this error but still haven't got any satisfactory answers.Kindly help
I am not sure about JBoss but for Glassfish there is an Ext directory for the Jars you want in the Classpath. So try to figure out how to deploy mysql-connector-java*.jar to your domain.
i know its late but, i believe i fixed this error by using a different factory class.
<property name="hibernate.transaction.factory_class" value="org.hibernate.ejb.transaction.JoinableCMTTransactionFactory"/>
hope it helps :)

where to use bean springTaskSessionFactory in JBPM process execution?

i got an example of spring configuration with jbpm here http://docs.jboss.org/drools/release/5.4.0.Final/droolsjbpm-integration-docs/html/ch.spring.html and i have implemented it.There is a bean called springTaskSessionFactory ,i don't get where this bean will be used in human task execution ?
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jbpm="http://drools.org/schema/drools-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://drools.org/schema/drools-spring org/drools/container/spring/drools-spring-1.2.0.xsd">
<!-- persistence & transactions-->
<bean id="htEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="org.jbpm.task" />
</bean>
<bean id="htEm" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="htEmf"/>
</bean>
<bean id="jpaTxMgr" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="htEmf" />
<!-- this must be true if using the SharedEntityManagerBean, and false otherwise -->
<property name="nestedTransactionAllowed" value="true"/>
</bean>
<bean id="htTxMgr" class="org.drools.container.spring.beans.persistence.HumanTaskSpringTransactionManager">
<constructor-arg ref="jpaTxMgr" />
</bean>
<!-- human-task beans -->
<bean id="systemEventListener" class="org.drools.SystemEventListenerFactory" factory-method="getSystemEventListener" />
<bean id="taskService" class="org.jbpm.task.service.TaskService" >
<property name="systemEventListener" ref="systemEventListener" />
</bean>
<bean id="springTaskSessionFactory" class="org.jbpm.task.service.persistence.TaskSessionSpringFactoryImpl"
init-method="initialize" depends-on="taskService" >
<!-- if using the SharedEntityManagerBean, make sure to enable nested transactions -->
<property name="entityManager" ref="htEm" />
<property name="transactionManager" ref="htTxMgr" />
<property name="useJTA" value="false" />
<property name="taskService" ref="taskService" />
</bean>
</beans>

Spring JPA/Hibernate in standalone app

I am utilizing spring in non web application and I am using hibernate for working with DB. Problem I am experiencing is that while "registerShutdownHook();" does close spring context container it does not properly shut down and close resources for JPA so my connections to DB are getting maxed out.
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="pu" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false" />
<property name="showSql" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
I use configuration presented above to fire up JPA layer and use "#Transactional" annotations to inject EM's into DAO's.
Maybe someone could help me out what am I missing or how should I handle proper closing of JPA sessions in standalone environment ?
Thank you,
P.S. Exception I am getting is: java.net.SocketException: No buffer space available (maximum connections reached?): connect
1.Create transaction manager as follow :
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
<property name="persistenceUnitName" value="persistanceUnit"/>
<property name="dataSource" ref="dataSource"/>
<property name="persistenceXmlLocation" value="classpath:persistence.xml"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="${db.orm.showsql}" />
<property name="generateDdl" value="${db.orm.generateDdl}" />
<property name="database" value="${db.type}"/>
<property name="databasePlatform" value="${db.orm.dialect}" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
2.use persistance.xml(should be in classpath)
<?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="persistanceUnit" transaction-type="RESOURCE_LOCAL">
<description>Oracle db Persistence Unit</description>
<class>com.company.YourModelClass</class>
<properties/>
</persistence-unit>
</persistence>
3.Add following annotation in applicationContext.xml
<context:component-scan base-package="com.yourcompany.basepackage" />
4.annoatate your Entitymanager in service class like:
#PersistenceContext
private EntityManager em = null;
5.Inject TrasnsactionManager to :
private PlatformTransactionManager platformTransactionManager = null;
6.persist object like:
platformTransactionManager .persist(obj);
This is another example of application Context.xml with JPA. It works fine for me.
<context:property-placeholder location=”classpath:jdbc.properties”/>
<!– Connection Pool –>
<bean id=”dataSource” destroy-method=”close”>
<property name=”driverClass” value=”${jdbc.driverClass}”/>
<property name=”jdbcUrl” value=”${jdbc.url}”/>
<property name=”user” value=”${jdbc.username}”/>
<property name=”password” value=”${jdbc.password}”/>
</bean>
<!– JPA EntityManagerFactory –>
<bean id=”entityManagerFactory”
p:dataSource-ref=”dataSource”>
<property name=”jpaVendorAdapter”>
<bean>
<property name=”database” value=”${jdbc.database}”/>
<property name=”showSql” value=”${jdbc.showSql}”/>
</bean>
</property>
</bean>
<!– Transaction manager for a single JPA EntityManagerFactory (alternative to JTA) –>
<bean id=”transactionManager”
p:entityManagerFactory-ref=”entityManagerFactory”/>
<!– Activates various annotations to be detected in bean classes for eg #Autowired–>
<context:annotation-config/>
<!– enable the configuration of transactional behavior based on annotations –>
<tx:annotation-driven transaction-manager=”transactionManager”/>
<!– Property Configurator –>
<bean id=”propertyConfigurer”>
<property name=”location” value=”jdbc.properties”/>
</bean>
<context:component-scan base-package=”com.test.dao”/>
</beans>

Resources