Websphere: JPA: java.lang.IllegalArgumentException: Object: Entity is not a known entity type - spring

I'm trying to deploy an app using Eclipselink as my JPA Layer on IBM Websphere 7.0.0.9. While trying to do any CRUD operations, i get the following exception:
Caused by: java.lang.IllegalArgumentException: Object: Entity is not a known entity type.
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4199)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:380)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:600)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:358)
at $Proxy93.persist(Unknown Source)
... 89 more
Another stack trace i see is :
Caused by: java.lang.ClassCastException: Entity incompatible with Entity
at o.u.d.dao.jpa.converter.impl.EntityBeanConvertorImpl.convertToModel(EntityBeanConvertorImpl.java:143)
... 223 more
My persistence.xml looks like:
<persistence-unit name="ds" transaction-type="JTA"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <jta-data-source>dsjta</jta-data-source> <class>Entity</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="eclipselink.target-server" value="WebSphere_7" /> <property name="eclipselink.logging.level" value="OFF" /> <property name="eclipselink.ddl-generation" value="none" /> <property name="eclipselink.ddl-generation.output-mode" value="database" /> </properties> </persistence-unit>
My application context file looks like:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitName" value="ds"/> <property name="dataSource" ref="dataSource"/> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"> <property name="showSql" value="false"/> <property name="generateDdl" value="false"/> </bean> </property> <property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/> </property> </bean>
Our VM is started with the -javaagent parameter specified and pointing to spring-agent.jar What could be the reason for these exceptions?

The issue here is there is no Spring LoadTimeWeaver for Websphere. When your application attempts to combine a Web-Tier application with the Spring managed EM while using the InstrumentationLoadTimeWeaver class cast exceptions result. EclipseLink supports weaving in all JPA 2 compliant containers but in this case Spring is acting as an intermediary and interfering with the weaving.
Until Spring has a LoadTimeWeaver for Websphere You will need to remove the InstrumentationLoadTimeWeaver and set the EclipseLink persistence.xml property "eclipselink.weaving" to false or use the Static weaver.

Related

Transaction not getting committed

Working on a Spring 5 and Hibernate Project. Using Spring for bean management, transaction and MVC. The changes are not getting committed to database though i can see the insert statement in the log. There is no error. There is no issue with select statements. I could login into the application. Below are my configurations:
framework.xml:
<context:component-scan base-package="com.test" >
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource"
ref="dataSource"/>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="jtaTransactionManager" ref="transactionManager" />
</bean>
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<property name="forceShutdown" value="false" />
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.J2eeUserTransaction">
<property name="transactionTimeout" value="300" />
</bean>
<!--
<bean id="HibernateTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
-->
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" >
<property name="transactionManager"><ref bean="atomikosTransactionManager" /></property>
<property name="userTransaction"><ref bean="atomikosUserTransaction" /></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/>
In the above configuration, i excluded the controllers as they are loaded using a different mvc related config files.
The service classes were annotated with #Transactional.
I tried without JTA with plain HibernateTransactionManager also. The transactions are not getting committed.
The below entry is also for hibernate to use JTA as transaction manager
hibernate.transaction.jta.platform to org.hibernate.engine.transaction.jta.
platform.internal.AtomikosJtaPlatform and
hibernate.transaction.coordinator_class to jta
I am using getCurrentSession for getting hibernate session.
I have to use Atomikos as JTA transaction manager as the development has to be happen in servlet container.
Thanks in advance for any help to find the gap in the configuration or any other issues..
The problem was with the maven Jetty plugin and the db connection created didn't persist the changes with hibernate 5 with maven jetty plugin 9.4.35.v20201120 though it was working with hibernate 3 and maven jetty plugin 7.0.1.v20091125. When I deployed the same war file with pre-configured data source in tomcat, able to persist the changes.

Spring data-jpa and exception handling

Recently I had been discovered Spring Data Jpa. The one thing I was not able to make it working was proper exception translation to Spring's Exception hierarchy.
According to this Spring Data JPA forces CGLib proxying to non repository classes the <jpa:repositories /> activates persistence exception translation for Spring beans annotated with #Repository. The reference documentation in this post points to spring-data-jpa 1.1.1.
But when you look at the docs for version 1.3.0 this paragraph has been removed. Also I was plying with #Repository annotation putting it wherever possible but with no success.
My question is: Is it possible to achieve proper exception translation with the recent spring-data-jpa lib version 1.3.0?
Ok. I will put some configuration here:
...
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="oracle.jdbc.OracleDriver"/>
<property name="jdbcUrl" value="jdbc:oracle:thin:#localhost:1521:pbase"/>
<property name="user" value="sa"/>
<property name="password" value="pass"/>
</bean>
<context:annotation-config/>
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="persistenceUnitName" value="prjPersistenceUnit"></property>
<property name="persistenceXmlLocation" value="classpath:META-INF/mpersistence.xml"></property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"
p:showSql="true"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf"/>
</bean>
<tx:annotation-driven/>
<jpa:repositories base-package="com.mycompany.repository" />
Content of mpersistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="prjPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<description>Persistence unit which uses EclipseLink JPA 2.0 implementation.</description>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.mycompany.Setting</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.target-server" value="JBoss"/>
<property name="eclipselink.target-database" value="Oracle10g"/>
<property name="eclipselink.weaving" value="static"/>
</properties>
</persistence-unit>
</persistence>
My rpository
#Repository
public interface TestRepository extends JpaRepository<Setting, Long> {
Setting findByNamee(String name);
}
Here findByNamee should rise some Spring database exception as a real property in the databese is name not namee. But I always get
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Though, when configuring a regular Dao object with #Repository annotation everything works as expected.
I am trying to deploy it on Tomcat 6.0 with eclipseLink 2.3.2.
You could check some working example code I published since then at https://github.com/zagyi/examples/tree/master/spring-data-jpa
You have to inject the EclipseLink Jpa Dialect to the EntityManagerFactory, because the exception translator is in the dialect.

Using jndi with jpa and spring on glassfish

I have a web application that runs on glassfish 3.1. I am using JPA (Hibernate) configured with spring.
This is my persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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_2_0.xsd"
version="2.0">
<persistence-unit name="myPU">
<class>org.myCompany.entities.Class1</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
This is part of my ApplicationContext.xml:
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory" />
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/myDataSource" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="myPU" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
</bean>
</property>
</bean>
In addition i have declared my datasource in the glassfish server (jdbc connection pool and recourse)
and the JNDI name is "jdbc/myDataSource". The issue is that on my local installation of glassfish, published via the glassfish plugin for eclipse, it works fine BUT when i deploy the war file manually on an installation of glassfish (i tried on my VPS - CentOS) then i get this error:
Error occurred during deployment: Exception while preparing the app : Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.SQLException: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused. Error Code: 0. Please see server.log for more details.
For some reason, the application is using the default derby datasource of glassfish and not my defined datasource. anyone knows why and how to overcome this?
Did you configure your JDBC Resource to use the right Connection Pool? There is a default derby pool configured. Maybe you just selected the wrong pool for your resource?

Exception while creating EntityManagerFactory - Lookup failed for 'persistence/myPU' in SerialContext

I'm working with Glassfish application server and trying to connect my spring-hibernate web application to my db, I have the following configurations:
In Glassfish I have added a jdbc connection and send a successful ping to the DB.
I also added a JDBC resource (in glassfish) with jndi name: jdbc/myResource. This resource is under the connection pool in which I have created and tested (in step 1).
My EntityManaget is annotated with #PersistenceContext:
#PersistenceContext(unitName = "myPU")
protected EntityManager entityManager;
Under \src\main\resources\META-INF\ I created my persistence.xml file:
<persistence-unit name="myPU">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/myResource</jta-data-source>
<properties>
<!-- Glassfish transaction manager lookup -->
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.SunONETransactionManagerLookup" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"></property>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.default_schema" value="PUBLIC" />
<property name="hibernate.format_sql" value="true" />
<!-- Validates the existing schema with the current entities configuration -->
<property name="hibernate.hbm2ddl.auto" value="validate" />
</properties>
</persistence-unit>
This is how my applicationContext looks like:
bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<jee:jndi-lookup id="emf" jndi-name="persistence/myPU" />
<!-- In order to enable EntityManager injection -->
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
<property name="persistenceUnits">
<map>
<entry key="myPU" value="persistence/myPU" />
</map>
</property>
</bean>
In my web.xml I have:
<persistence-unit-ref>
<persistence-unit-ref-name>persistence/myPU</persistence-unit-ref-name>
<persistence-unit-name>myPU</persistence-unit-name>
When I publish I keep getting:
No bean named 'myPU' is defined. Please see server.log for more details.
Why is it looking for the persistence unit as a bean?
Any ideas what am I doing wrong?
Thanks in advance
Resolved - I added transaction-type="JTA" to my persistence.xml.
For some reason I was under the impression that if I'm specifying a jta-data-source in my persistence.xml the default transaction-type should be "JTA", maybe it isn't. I don't really understand the relation of the exception I had to this solution but at the moment my persistence.xml has the following line:
<persistence-unit name="myPU" transaction-type="JTA">
And it works like a charm.

EJB 3.0 -> Spring -> JPA (JTA as transaction manager)

I am currently working on a project that includes EJB 3.0 (stateless SB), JPA (Hibernate as the provider), JTA as transaction manager. The app server is JBoss AS 7. Spring is used for integrating EJB and JPA.
All seems to be working fine, except if there is any exception that occurs in the EJB, then the persistence unit is closed by Spring. On the subsequent request, the persistence unit is again created, which becomes time consuming and also should not happen in the ideal situation.
Below are the configuration details
persistence.xml
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<class>com.test.User</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
spring-application-context.xml
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<jee:jndi-lookup id="dataSource" jndi-name="java:/datasources/test" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false" />
<property name="database" value="MYSQL" />
<property name="showSql" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"></entry>
<entry key="hibernate.current_session_context_class" value="jta" />
<entry key="hibernate.connection.release_mode" value="auto" />
</map>
</property>
<property name="persistenceUnitPostProcessors">
<list>
<bean class="com.transaction.processor.JtaPersistenceUnitPostProcessor">
<property name="jtaMode" value="true"/>
<property name="jtaDataSource" ref="dataSource"/>
</bean>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName" value="java:/TransactionManager"></property>
<property name="autodetectUserTransaction" value="false"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
The class JtaPersistenceUnitPostProcessor is responsible for setting the transaction-type as JTA and the datasource to jta-datasource.
Could anyone please provide any help on this.
Thanks in advance.
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName" value="java:jboss/TransactionManager" />
<property name="userTransactionName" value="java:comp/UserTransaction" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
you didn't specify any error message . you can add these lines in your configuration file .
I see you use JTA transaction manager and use that only if you use distributed Transaction and use JNDI. JTA tran. manager listens TX happening through connection acquired from JNDI datasource. If you have datasource created in your code and is not a part of Web container but is limited inside app. container in your web server, JTA wont work.
If you want to implement Tx manager with in a single app. context go for JPA transaction manager which is very reliable.

Resources