Persistence deployment issue - java

I have a hibernate project, which uses JPA.
my persistence.xml contents is as follows:
<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="Demo-PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>java:/DemoDS</non-jta-data-source>
<class>com.demo.framework.entity.ReportDefinitionEntity</class>
<properties>
<!-- Database connection -->
<property name="hibernate.connection.url" value="jdbc:mysql://192.168.9.110:3306/demoDB" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="root" />
<!-- Hibernate dialect -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<!-- Output goodies
-->
<property name="hibernate.query.jpaql_strict_compliance" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.use_sql_comments" value="false" />
<!-- Cache
-->
<property name="hibernate.jdbc.batch_versioned_data" value="true" />
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
</properties>
</persistence-unit>
</persistence>
Now when I run it using eclipse I don't have a problem, but when I deploy it in Jboss, I get the below error:
ERROR [AbstractKernelController] Error
installing to Start:
name=persistence.unit:unitName=#Demo-PU
state=Create
java.lang.ClassCastException:
org.hibernate.ejb.HibernatePersistence
cannot be cast to
javax.persistence.spi.PersistenceProvider
And here is the list of Jar that I have
activation.jar
antlr-2.7.6.jar
asm-attrs.jar
asm.jar
cglib-2.1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.1.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-entitymanager.jar
hibernate-tools.jar
hibernate3.jar
javassist.jar
javax.persistence.jar
jdbc2_0-stdext.jar
jta.jar
mysql-connector-java-5.0.5-bin.jar
xml-writer.jar
How can I resolve this issue?

The ClassCastException is caused by having two copies of the javax.persistence APIs in your system (one in the common classloader provided by JBoss and the one in your app). When running on JBoss, you are just not supposed to provide this API in your application, don't package it.
By the way, it seems you're using a JPA 2.0 persistence.xml but I'm not convinced you're using the JPA 2.0 implemenation of Hibernate (actually, you seem to be using a pretty old version since I can see commons-logging.jar). You should probably fix that i.e. use the 1.0 version of persistence.xml.
Actually, you should very likely use a different persistence.xml when running on JBoss (using a JTA entity manager and a jta-data-source). And it seems weird to mix data source usage and Hibernate built-in connection pool.

Related

Weblogic 12 get eclipse error when tried to deploy

When i tried to deploy an application in weblogic, he give-me the follow exception:
Substituted for missing class Exception [EclipseLink-7298] (Eclipse
Persistence Services - 2.6.5.v20170607-b3d05bd) -
org.eclipse.persistence.exceptions.ValidationException Exception
Description: The mapping [associated] from the embedded ID class
[class br.com.webproj.web.model.associated.pk.AssmaintainPK] is an
invalid mapping for this class. An embeddable class that is used with
an embedded ID specification (attribute [AssmaintainPK] from the
source [class br.com.webproj.web.model.associated.AssMaintain]) can
only contain basic mappings. Either remove the non basic mapping or
change the embedded ID specification on the source to be embedded.
In my persistence.xml i have:
<?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="Web_PU">
<jta-data-source>JDBC/WEB</jta-data-source>
<jar-file>web-Model.jar</jar-file>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.hbm2ddl.auto" value="none" />
<property name="hibernate.show_sql" value="false" />
<!-- Cache Enabled -->
<property name="hibernate.cache.use_second_level_cache"
value="true" />
<property name="hibernate.cache.use_query_cache"
value="true" />
<property name="hibernate.cache.region.factory_class"
value="org.hibernate.cache.infinispan.JndiInfinispanRegionFactory" />
<property name="hibernate.cache.infinispan.cachemanager"
value="java:jboss/infinispan/container/hibernate" />
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.JBossTransactionManagerLookup" />
<property name="hibernate.cache.infinispan.statistics"
value="true" />
<!-- I TRIED INCLUDING THIS, BUT NOT SOLVE -->
<property name="eclipselink.exclude-eclipselink-orm"
value="false" />
<property name="eclipselink.orm.throw.exceptions"
value="false" />
<property name="eclipselink.orm.validate.schema"
value="false" />
</properties>
</persistence-unit>
</persistence>
I think this error occurs because the weblogic use the eclipse link, and the project an other version of jpa. But the error is in a jar included in project and i cant change and see the code. If the problem is this, what i can do to dont use the eclipse link of weblogic (obs: in JBoss the project runs normally).

How to use DB2 JDBC driver with Hibernate

I keep getting
java.lang.ClassNotFoundException: Could not load requested class : com.ibm.db2.jcc.DB2Driver
when trying to connect to a DB2 database using Hibernate. The driver jar is referenced as an external library:
Image of Eclipse's "Referenced Library" folder
It also shows up in the classpath:
classpathentry kind="lib" path="C:/Program Files (x86)/IBM/SQLLIB/java/db2jcc.jar"/>
I can also access the class by importing it in the source code. My persistence.xml looks as follows:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="name" transaction-type="RESOURCE_LOCAL">
<description>Persistence Unit</description>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>MyClass</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.ibm.db2.jcc.DB2Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:db2://url" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
My suspicion why this does not work is that the class is contained in an external library and not as a maven dependency, because when I replace the DB2 driver by net.ucanaccess.jdbc.UcanaccessDriver (which is contained in a Maven package), the class will be found just fine.
Any idea what I am doing wrong here?
when you are using com.ibm.db2.jcc.DB2Driver' make sure db2jcc.jar & db2jcc_license_cu.jar are in your classpath.Please add both the jar in your classpath and give a try.
The problem was that I was using the Maven exec plugin to run the main class. W/o using Maven, it works fine...

Environment specific Persistence xml in java standalone application

I have a standalone java application, which uses JPA for its persistence.
Right now I have a persistence.xml in META-INF.My application is currently in development.
My question is that if I move from development to the next envirnoment, say QA. I have to modify the persistence.xml and rebuild the jar. Is this the right way to go about it ?
If not,if I move the connection properties to a different file, where should this file be placed?
<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="pu1" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>ClassA</class>
<class>ClassB</class>
<class>ClassC</class>
<class>ClassD</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.connection.username" value="username" />
<property name="hibernate.connection.password" value="password" />
<property name="hibernate.connection.url"
value="url" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.archive.autodetection" value="class" />
</properties>
</persistence-unit>
</persistence>
Thanks in advance !
That's a good question. Normally, you put all these environment settings in an external file, say application.properties, and pass the location to it to the JVM when you start your application (e.g. -Dconfig.location=/conf/)
Then you should find a way to get the externalized properties into your EntityManagerFactory. You can't do that in persistence.xml, you can only hard-code things there. But you can do it when creating the entity manager factory by passing vendor properties.
If using a framework like spring, for example, this is easier to do, as spring provides a factory bean for the entity manager. Otherwise you should handle it yourself. Here's the relevant bit from spring:
provider.createEntityManagerFactory(persistenceUnitInfo, getJpaPropertyMap())

EclipseLink into OSGI Bundle persistence.xml not found

I'm having some problem with EclipseLink. I'm using GlassFish v3.1 and I'm trying to use EclipseLink for my persistence layer. I followed all tutorials available on the Eclipse wiki without luck. My persistence.xml file cannot be parsed and I receive this error while trying to create the EntityManagerFactory:
org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while processing persistence.xml from URL: bundle://307.1:1/
Here is my persistence.xml located in /WEB-INF/classes/META-INF/:
<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="generic">
<class>com.generic.domain.Service</class>
<properties>
<!-- Embedded MySQL Login -->
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306"/>
<!-- TODO: replace with connection pool -->
<property name="javax.persistence.jdbc.userid" value="root"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.target-database" value="MySQL"/>
<property name="eclipselink.jdbc.read-connections.min" value="1"/>
<property name="eclipselink.jdbc.write-connections.min" value="1"/>
<property name="eclipselink.jdbc.batch-writing" value="JDBC"/>
<!-- Logging Settings -->
<property name="eclipselink.logging.level" value="FINE" />
<property name="eclipselink.logging.thread" value="false" />
<property name="eclipselink.logging.session" value="false" />
<property name="eclipselink.logging.exceptions" value="true" />
<property name="eclipselink.logging.timestamp" value="false"/>
</properties>
</persistence-unit>
</persistence>
I added this line to my MANIFEST.MF:
JPA-PersistenceUnits: generic
I can now confirm that it's a bug in EclipseLink. The work around to your problem is to either get hold of EntityManagerFactory using JNDI lookup or #PersistenceUnit instead of doing Persistence.createEntityManagerFactory().
1) It looks like you are trying to use OSGi/JPA from your WAB. Correct?
2) Can you tell if you have installed any new eclipselink bundles in your system? If so, what are they?
3) Can you provide stack trace?

Hibernate/JPA: Mapping entities to different databases

I have an application which manages 3 databases. I use hibernate with JPA on seam framework.
So I have a persitence.xml file with three persitence-unit like this (I remove properties for db2 and db3):
<persistence-unit name="db1" transaction-type="JTA" >
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>db1source</jta-data-source>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.connection.driver_class"
value="oracle.jdbc.driver.OracleDriver" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.default_schema" value="SI_TEC" />
<property name="hibernate.validator.apply_to_ddl" value="false" />
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.WeblogicTransactionManagerLookup" />
</properties>
</persistence-unit>
<persistence-unit name="db2" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>d2source</jta-data-source>
</persistence-unit>
<persistence-unit name="db3" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>d3source</jta-data-source>
</persistence-unit>
In my seam components.xml file, I create 3 managed-persistence-context to map seam with my hibernate configuration.
Finally, I have several entities and my problem is here. I need to persist some entities in db2 and other in db3. So database schema are different and when I deploy my application, I get this error:
org.hibernate.HibernateException: Missing table: PORTAILPERMISSION
at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1113)
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:139)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:349)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
Truncated. see log file for complete stacktrace
Because, the table PORTAILPERMISSION doesn't exist in db2.
My question is:
How to specify in entity class what database (or persitence-unit) must be used to validate entity in startup ?
Thanks for your help.
You try to explicitly list classes (<class>..</class>) in each persistence unit. And use
<exclude-unlisted-classes>true</exclude-unlisted-classes>

Categories

Resources