Entities not being detected when using packaged Hibernate JPA persistence provider - java

Im deploying my web application on a local wildfly-11.0.0.Final
server.
Wildfly however provides its own hibernate and jpa modules which I
dont want to use. I want to use the jpa jars packaged with my
application.
As described in
https://docs.jboss.org/author/display/WFLY10/JPA+Reference+Guide#JPAReferenceGuide-PackagingtheHibernateJPApersistenceproviderwithyourapplication
I added the line
<property name="jboss.as.jpa.providerModule" value="application"/>
to my persistence.xml
Now however my entites (Annotated with #Entity) are not being detected anymore and I have to explicitly name them in my persistence.xml like
<class>com.mycompany.mywebapp.Actor</class>
Is there any way to fix this?
I tried
<jar-file></jar-file>
and
<property name="hibernate.archive.autodetection" value="class, hbm"/>
without success.
My persistence.xml looks like this:
<?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="com.mycompany_mywebapp_war_1.0PU" transaction-type="JTA">
<jta-data-source>java:/jboss/sakila</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="jboss.as.jpa.providerModule" value="application"/>
</properties>
</persistence-unit>
</persistence>
Edit:
Im using hibernate 5.3.7.Final which implements jpa 2.2.
My wildfly server provides hibernate 5.1.10.Final with jpa 2.1.
Edit 2:
I noticed that even though the hibernate version is now 5.3.7.Final the jpa provided by wildfly (specification version 2.1) is still being used which may be causing this issue.
I tried disabling the wildfly modules in a jboss-deployment-structure.xml:
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name="org.hibernate" />
<module name="javax.persistence.api" />
</exclusions>
</deployment>
</jboss-deployment-structure>
However now im facing following error:
Cannot upload deployment: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"webapp-1.0.war\".FIRST_MODULE_USE" => "WFLYSRV0153: Failed to process phase FIRST_MODULE_USE of deployment \"webapp-1.0.war\" Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: WFLYJPA0019: Could not deploy application packaged persistence provider 'org.hibernate.jpa.HibernatePersistenceProvider' Caused by: java.lang.ClassCastException: class org.hibernate.jpa.HibernatePersistenceProvider"}}
Edit 3:
To test whether this is the cause for my initial problem I manually replaced the jpa jar. Unfortunately this doesnt solve my problem.
Anyway I would like to know how I can fix the issue of my packaged jpa jar not being used and how to make my entities be automatically detected again. Any hints?

Related

Configuration of persistence.xml while using websphere 8.5.5.1

I'm coding an app that uses Servlet 3.0, Jsp, and JPA 2.0 and i'm deploying it into Websphere application server 8.5.
Since i already configured into the ibm websphere console, the data source and the jdbc driver, and the j2c authentification (i'm using oracle 11g as a database ). I dont know how my persistence.xml should look like, if i need to specify and add openJPA jars to my project.
For now anything i put into persistence.xml i'm having this issue :
Error 500: <openjpa-2.2.3-SNAPSHOT-r422266:1764177 fatal user error> org.apache.openjpa.persistence.ArgumentException
What should i do ? maybe i'm missing how JPA works
Thanks in advance
The OpenJPA jars should be provided by WebSphere and available to use for your application. There is a JPA sample available here: https://developer.ibm.com/wasdev/downloads/#asset/samples-Java_Persistence_API_JPA_Sample
In the sample, you can see an example of the persistence.xml file:
<?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="jpasamplepersistenceunit">
<jta-data-source>java:comp/env/jdbc/samplejpadatasource</jta-data-source>
<non-jta-data-source>java:comp/env/jdbc/samplejpadatasourcenonjta</non-jta-data-source>
<class>wasdev.sample.jpa.Thing</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- These properties are creating the database on the fly. We are using them to avoid users having
to create a database to run the sample.
See also the create=true line in the datasource meta data. -->
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
<property name="openjpa.jdbc.DBDictionary" value="derby" />
<!-- EclipseLink specific properties to create the database. They are only used if using the jpa-2.1 feature. -->
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="both" />
</properties>
</persistence-unit>
</persistence>
I think your error is not caused by the OpenJPA jars not being available. It might be because your database is not configured correctly. Make sure your persistence.xml file refers to your datasources properly.

What's the right way to use Wildfly 10 with Hibernate and MongoDB?

I'm new to this, did a lot of research now and still I'm not able to develop a working Wildfly / Hibernate / MongoDB Environment.
Here is basically did so far:
Read this Guide
Download Wildfly 10
Download Hibernate OGM 5.1.0.Final and ORM 5.1.4 Modules Archive
Extract Modules into wildfly/modules
Download/Install MongoDB (i tried 2 Versions)
via Brew on Mac
via Ubuntu in an VM
Create new Maven Web Project in Netbeans
Next Step is to edit pom.xml, jboss-deployment-structure.xml, persistence.xml. And that's the Point where the trouble begins. I tried different entries and get different errors. Right now my configuration looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<dependencies>
<module name="org.hibernate.ogm" slot="5.1" services="export" />
<module name="org.hibernate.ogm.mongodb" slot="5.1" services="export" />
</dependencies>
</deployment>
</jboss-deployment-structure>
I saw different Versions of this file, sometimes with slot="main", sometimes with services="import".
Persitence.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="comic-PU" transaction-type="JTA">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="jboss.as.jpa.providerModule" value="org.hibernate:5.1"/>
<property name="hibernate.ogm.datastore.provider" value="mongodb"/>
<property name="hibernate.ogm.datastore.database" value="comicDB"/>
<property name="hibernate.ogm.datastore.host" value="localhost"/>
<property name="hibernate.ogm.datastore.port" value="27017"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
I'am pretty sure here is something wrong. When deploying the Project, Wildfly tries to read persistence.xml, then stuck for a moment and:
20:58:33,255 INFO [org.jboss.as.jpa] (MSC service thread 1-6) WFLYJPA0002: Read persistence.xml for comic-PU
21:03:32,767 ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 1) WFLYCTL0348: Timeout after [300] seconds waiting for service container stability. Operation will roll back. Step that first updated the service container was 'deploy' at address '[("deployment" => "MYAPP.war")]'
21:03:32,772 ERROR [org.jboss.as.server] (DeploymentScanner-threads - 1) WFLYSRV0022: Deploy of deployment "ComicFeeder-1.0.war" was rolled back with no failure message
Pom.xml is mostly auto-generated, i added
<dependency>
<groupId>org.hibernate.ogm</groupId>
<artifactId>hibernate-ogm-mongodb</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.ogm</groupId>
<artifactId>hibernate-ogm-core</artifactId>
<version>5.1.0.Final</version>
</dependency>
<archive>
<manifestEntries>
<mode>development</mode>
<url>${project.url}</url>
<Dependencies>org.hibernate.ogm:5.1 services, org.hibernate.ogm.mongodb:5.1 services</Dependencies>
</manifestEntries>
</archive>
Is the manifest entry necessary?
An Errors I got:
Wildfly: org.hibernate.search.engine 5.6.1.Final not found
in wildfly/modules/org/hibernate/search/engine the modules name is "5.6.1.Final-orm51" -> renamed it to 5.6.1.Final, edit the Entry in 5.6.1.Final/modules.xml
Wildfly then starts without that error
This looks like a bug to me
Where is my mistake? What's your "workflow" when you start a new project with hibernate, wildfly and mongodb?
Would it be easier to use Glassfish or any other Server?
Some Tutorials I found are outdated or just did not work (for me).

JavaEE + eclipseLink + TomEE gives java.sql.SQLSyntaxErrorException: user lacks privilege or object not found

I try to make a simple app using a rest service, eclipseLink and mysql.
I want to make this run on a TomEE server (apache-tomee-plume-1.7.4).
I deploy the app with eclipse.
The deployment seems to be ok
When I go to http://localhost:8080/eleve/ I'm getting :
javax.servlet.ServletException: Error processing webservice request
org.apache.tomee.webservices.CXFJAXRSFilter.doFilter(CXFJAXRSFilter.java:98)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
java.io.IOException: Failed to invoke AbstractHTTPDestination
org.apache.openejb.server.cxf.rs.CxfRsHttpListener.doInvoke(CxfRsHttpListener.java:229)
org.apache.tomee.webservices.CXFJAXRSFilter.doFilter(CXFJAXRSFilter.java:94)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
java.lang.RuntimeException: org.apache.cxf.interceptor.Fault:
Internal Exception: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: ELEVE
Error Code: -5501
Call: SELECT ID, ADRESSE, classe, date_naissance, NOM, PRENOM, SEXE FROM ELEVE
Query: ReadAllQuery(referenceClass=Eleve sql="SELECT ID, ADRESSE, classe, date_naissance, NOM, PRENOM, SEXE FROM ELEVE")
org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:116)
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:324)
org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:240)
org.apache.openejb.server.cxf.rs.CxfRsHttpListener.doInvoke(CxfRsHttpListener.java:227)
org.apache.tomee.webservices.CXFJAXRSFilter.doFilter(CXFJAXRSFilter.java:94)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Here is my persistence.xml:
<?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="notePU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.test.eleve.model.Eleve</class>
<properties>
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/notes_eleves" />
<!-- <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> -->
<property name="eclipselink.logging.level" value="INFO" />
</properties>
</persistence-unit>
</persistence>
I must be missing something but I can not find what.
I pushed my code here: gitlab
Thanks for your help
I finally managed to make it works on TomEE 1.7 and 7.
This is the changes I had to do:
Remove the database related properties from the persistence.xml
Replace non-jta-data-source tag by jta-data-source in persistence.xml
Put the mysql connector jar in my server /lib
In my server /conf/tomee.xml I have added myDatasource configuration as a resource.
Put #XmlRootElement(name = "eleve") over my Eleve entity (this seems to be only mandatory on TomEE <7)
At the end I think that my issue was that the datasource needs to be configure in the server conf in a EE context (datasource properties in persistence was just ignore I think, so it was like no one was declared) and the exception
user lacks privilege or object not found
was comming from the fact that:
If a DataSource is needed by the application and one is not declared,
TomEE will create one dynamically using default settings.
TomEE documentation
I'm not 100% sure of that explanation but at least the problem is solved, don't hesitate to put comments if I misunderstood something.
I have updated the gitlab project
Edit: Be aware that you can also configure the resource in a /WEB-INF/resources.xml file
Edit 2: If you are using Eclipse you can also face this issue if you wrongly configured your server location, it should be set to "use Tomcat installation (take control...)" and not "use workspace metadata"

Persistence unit name issue during deployment WildFly

Stuck with some issue with my persistence module.
getting error like "Unexpected problem gathering statistics: java.lang.IllegalStateException: JBAS011477: Persistence unit 'EAR_FileName.ear/EJB_Module_Persistence.jar#MyPersistenceUnit' is not available"
my persistence.xml file is located as highlighted below. please correct me if i am wrong - as per packaging structure persistence unit name starting with EAR file name then my persistence module name then #unit name, which is absolutely right. and i am injecting my persistence unit in another ejb using PersistenceContext.
#PersistenceContext(unitName="MyPersistenceUnit")
private EntityManager em;
could anyone suggest me what wrong i am doing here. i would appreciate any input/help you could provide on this.
here is below module structure of my project
EAR_FileName.ear
|
|---EJB_Module1.jar
|
|---EJB_Module_Persistence.jar
|
|---META-INF
|
|---persistence.xml
Here is my persistence.xml file
<?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="MyPersistenceUnit" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/MyTransactionDS</jta-data-source>
<class>all class</class>
<properties>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
</properties>
</persistence-unit>
</persistence>
P.S. using WildFly 8.1 as an application server.
thanks in advance.
It looks that there is already a defect at the Wildfly-Tracker for this problem:
WFLY-4908: Redeploy dependent ear fails with duplicate resource error for persistence unit
There is as well a post at the Jboss-Forum:
JBoss-Dev: Redeploy dependent ear fails with duplicate resource error for persistence unit
According to the defect description, the issue is fixed with Wildfly 10.1.0.CR1.

No Entity metadata found for the class

I'm back with the same problem ...
I'm trying to uses queries in my Cassandra DB with Kundera (Cassandra ORM), this queries work in an others project but when I try to do it in webapp (using tomcat 6.0), I got this error :
com.impetus.kundera.metadata.KunderaMetadataManager - No Entity metadata found for the class
=> JavaNullPointerException.
But when I leave the persistence.xml from my project I got an other error. (NoPersistence.xml found or something ... )
So, my project found Persistence.xml, but not my Entity class : fileCassandra.
You can see 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">
<!-- 192.168.3.107 -->
<persistence-unit name="cassandra_pu">
<provider>com.impetus.kundera.KunderaPersistence</provider>
<class>net.***.common.db.***.FileCassandra</class>
<properties>
<property name="kundera.nodes" value="localhost"/>
<property name="kundera.port" value="9160"/>
<property name="kundera.keyspace" value="KunderaExamples"/>
<property name="kundera.dialect" value="cassandra"/>
<property name="kundera.client.lookup.class" value="com.impetus.client.cassandra.pelops.PelopsClientFactory" />
<property name="kundera.cache.provider.class" value="com.impetus.kundera.cache.ehcache.EhCacheProvider"/>
<!-- <property name="kundera.cache.config.resource" value="/ehcache-test.xml"/> -->
</properties>
</persistence-unit>
</persistence>
net..common.db..FileCassandra I must replace by * because it's name from my companie ;)
The same methods (include EntityManager) works in junit on other project, when I build my project in Tomcat, this error appears ...
This happens when you have multiple entries of the same class in your classpath.
The ideal place to have your entities is closest to the same class loader which loads kundera core and client(HBase, Cassandra etc.).
For example, if these kundera files are under WEB-INF/lib, you'd rather have your entities under the application where as if kundera files are on the applications lib folder, better bundle your entities in a jar and put them there (and remove the entities in your app).
Only issue which i can see is classes and persistence.xml location.
try to place persistence.xml within /WEB-INF/classes/META-INF/, Provided that your entity definitions are within classes folder!
-Vivek

Categories