I have developed a JAVA Rest Service (JDK 1.8) which uses Kundera (V3.2) for a Cassandra Database connection. If I run my application inside eclipse with a Tomcat-Server everything works well. If I want to deploy .war file on another Tomcat 7 the server does not start because of following error:
com.impetus.kundera.loader.MetamodelLoaderException: Error while retreiving and storing entity metadata, Caused by : .
After searching for some solution I found out that the problem can be that I have my entities in an separated jar (which is a dependency in the war file) and and not in the application itself. If this is the problem I have to add the <jar-file> tag to my persistence.xml (https://github.com/impetus-opensource/Kundera/issues/90).
My 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="cassandra_pu" transaction-type="RESOURCE_LOCAL">
<provider>com.impetus.kundera.KunderaPersistence</provider>
<class>com.asdf.booking.beans.kundera.Account</class>
<class>com.asdf.booking.beans.kundera.AccountCard</class>
<class>com.asdf.booking.beans.kundera.AccountType</class>
<class>com.asdf.booking.beans.kundera.Booking</class>
<class>com.asdf.booking.beans.kundera.Circle</class>
<class>com.asdf.booking.beans.kundera.AccountSequence</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="kundera.nodes" value="1.1.1.1" />
<property name="kundera.port" value="9160" />
<property name="kundera.keyspace" value="booking" />
<property name="kundera.dialect" value="cassandra"/>
<property name="sessionless" value="false" />
<property name="kundera.client.lookup.class" value="com.impetus.client.cassandra.thrift.ThriftClientFactory" />
<property name="kundera.annotations.scan.package" value="com.asdf.booking.kundera.beans" />
<property name="kundera.ddl.auto.prepare" value="update" />
</properties>
</persistence-unit>
</persistence>
Is the separate jar-file possibly the problem or should I search for other solutions?
The problem was the separate jar-file for entities. When adding the jar-file tag, pointing to the jar for entities, to the persistence.xml everything is ok.
Related
I am migrating a web application from Tomcat to Wildfly.
The application was using a configuration file stored under tomcat.
I want to avoid having configuration files and point the application to read these files.
I am looking to have the configuration read from Wildfly system like standalone.xml for instance.
what is the best approach?
Thanks
Please use persistence.xml file in Java app for definition make connection to database from standalone.xml file from WildFly
Excample persistence.xml - connection is get from WildFly. Please add JNDI name java:/PostgresDS in WildFly
<persistence 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"
version="2.1">
<persistence-unit name="em1">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:/PostgresDS</jta-data-source>
<properties>
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
I have a newbie question, so I hope you can help.
Currently as ORM tool I am using OpenJPA 2.2.1 .
MY CUrrent persistence UNit:
<?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" version="1.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="FleaCircus" transaction-type="JTA">
<description>Flea Circus</description>
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>FleaCircusOracleDS</jta-data-source>
<class>de.carmedialab.db.entities.ApplicationItem</class>
<class>de.carmedialab.db.entities.FleaResult</class>
<class>de.carmedialab.db.entities.FleaResultType</class>
<class>de.carmedialab.db.entities.ItemAttribute</class>
<class>de.carmedialab.db.entities.ItemGroup</class>
<class>de.carmedialab.db.entities.ItemType</class>
<class>de.carmedialab.db.entities.ItemTypeAttribute</class>
<class>de.carmedialab.db.entities.ItemTypeOperationAttribute</class>
<class>de.carmedialab.db.entities.Operation</class>
<class>de.carmedialab.db.entities.OperationAttribute</class>
<class>de.carmedialab.db.entities.OperationType</class>
<class>de.carmedialab.db.entities.Role</class>
<class>de.carmedialab.db.entities.UserAccount</class>
<class>de.carmedialab.db.entities.Measurement</class>
<class>de.carmedialab.db.entities.MeasurementType</class>
<class>de.carmedialab.db.entities.MeasurementAttribute</class>
<class>de.carmedialab.db.entities.MeasurementAttributeType</class>
<class>de.carmedialab.db.entities.Fleet</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="validate" />
<property
name="openjpa.Compatibility.CheckDatabaseForCascadePersistToDetachedEntity"
value="true" />
<!--<property name="openjpa.Log"
value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE" />-->
</properties>
</persistence-unit>
<persistence-unit name="FleaCircusLocal" transaction-type="RESOURCE_LOCAL">
<description>Flea Circus</description>
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<non-jta-data-source>FleaCircusOracleDS</non-jta-data-source>
<class>de.carmedialab.db.entities.ApplicationItem</class>
<class>de.carmedialab.db.entities.FleaResult</class>
<class>de.carmedialab.db.entities.FleaResultType</class>
<class>de.carmedialab.db.entities.ItemAttribute</class>
<class>de.carmedialab.db.entities.ItemGroup</class>
<class>de.carmedialab.db.entities.ItemType</class>
<class>de.carmedialab.db.entities.ItemTypeAttribute</class>
<class>de.carmedialab.db.entities.ItemTypeOperationAttribute</class>
<class>de.carmedialab.db.entities.Operation</class>
<class>de.carmedialab.db.entities.OperationAttribute</class>
<class>de.carmedialab.db.entities.OperationType</class>
<class>de.carmedialab.db.entities.Role</class>
<class>de.carmedialab.db.entities.UserAccount</class>
<class>de.carmedialab.db.entities.Measurement</class>
<class>de.carmedialab.db.entities.MeasurementType</class>
<class>de.carmedialab.db.entities.MeasurementAttribute</class>
<class>de.carmedialab.db.entities.MeasurementAttributeType</class>
<class>de.carmedialab.db.entities.Fleet</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="validate" />
<property
name="openjpa.Compatibility.CheckDatabaseForCascadePersistToDetachedEntity"
value="true" />
<!--<property name="openjpa.Log"
value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE" />-->
</properties>
</persistence-unit>
</persistence>
For the sake of simplicity, persistence-unit "FleaCircus" is what I use ( Container Managed Persistence ). FleaCircusLocal is another test pu I created , where I've created a sample stateless bean that basically starts a new transaction, persist an element and commit it.
However it doesn't sinchronize immediatly and customer complains about it.
Is there any way to make OpenJPA makes whats in context immediatly visible to the DB or is it impossible due to the nature as OpenJPA/JPA was thought ?
For me it is not a big deal since data is visible inside the container ( Apache Geronimo ) however for the customer it is important since he wants to connet using 3rd party tool ( eg. SQL Developer to and see if data was inserted or not .)
I'm creating an OpenJPA application with maven.
The application starts, but if I try to access a database-entity, I get the following exception:
HTTP Status 500 - <openjpa-2.2.2-r422266:1468616 fatal user error> org.apache.openjpa.persistence.ArgumentException: The persistence provider is attempting to use properties in the persistence.xml file to resolve the data source. A Java Database onnectivity (JDBC) driver or data source class name must be specified in the openjpa.ConnectionDriverName or javax.persistence.jdbc.driver property. The following properties are available in the configuration: "org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl#442ce698".
The persistence.xml is located in META-INF and is under control by Netbeans 8.
The ConnectionDriverName is set as the following:
<property name="openjpa.ConnectionDriverName" value="org.apache.commons.dbcp.BasicDataSource"/>
My src\main\java\META-INF\persistence.xml (class-tags reduced and database-settings changed):
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="PersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>com.example.test.my.entities.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/>
<property name="openjpa.Log" value="DefaultLevel=INFO, Tool=INFO, SQL=TRACE"/>
<property name="openjpa.jdbc.DBDictionary" value="org.apache.openjpa.jdbc.sql.MySQLDictionary"/>
<property name="openjpa.ConnectionDriverName" value="org.apache.commons.dbcp.BasicDataSource"/>
<property name="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=80, PrintParameters=true"/>
<property name="openjpa.ConnectionProperties" value="DriverClassName=com.mysql.jdbc.Driver,
Url=jdbc:mysql://localhost:3306/database?autoReconnect=true,
Username=usr,
Password=pwd,
MaxWait=60000,
TestOnBorrow=true,
validationQuery=select 1"/>
</properties>
</persistence-unit>
</persistence>
I'm using Tomcat 8 (same result for Tomcat 7 or Tomee).
solved it by switching to eclipse, because it seems like netbeans having some problems with the persistence.xml.
In eclipse I had to use the following m2e-connector:
https://github.com/hwellmann/m2eclipse-extras/raw/master/p2/ (eclipse software repository url)
I'm trying to use jpa with tomcat, with Derby. But I get this message : Caused by: ERROR XSDB6: Another instance of Derby may have already booted the database.
My persistence.xml is something like that :
<?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="contribs" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:~/contribs;create=true"/>
<property name="javax.persistence.jdbc.password" value="sa"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
I use a ServletContextListener, annotated with #WebListener().
On contextInitialized I get a EntityManagerFactory and an EntityManager, and I close all on contextDestroyed.
I see with log tomcat call twice my ServletContextListener (I don't understand why), and the second time I get the error message.
Also I use Netbeans.
How can I resolve that ? Thanks.
Are you hot-redeploying your application into Tomcat? Tomcat appears to frequently keep multiple copies of the web-app running, in separate application class loaders, and only one copy of your web-app is able to open Derby at a time.
One possibility is to switch from the Embedded version of Derby to the Client-Server version.
Another possibility is to restart Tomcat when the problem occurs.
I've just migrated from Tomcat to JBoss AS 7.
So, I configured Mysql datasource in JBoss (adding module.xml with associated Jar, adding driver bloc into standalone.xml and configuring datasource through JBoss interface.
No errors when deploying but impossible to get an entityManager (JPA with Hibernate in background).
Indeed, when this code is executed:
Persistence.createEntityManagerFactory("RoomManagement");
I obtain this error :
javax.persistence.PersistenceException: No Persistence provider for
EntityManager named RoomManagement
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
Very strange because I well verified that my persistence.xml does take place into War at WEB-INF/classes/META-INF directory.
My persistence.xml looks like as follow :
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="RoomManagement" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/MySqlDS</jta-data-source>
<class>com.parisdescartes.roommanagement.domain.entities.Address</class>
<class>com.parisdescartes.roommanagement.domain.entities.Building</class>
<class>com.parisdescartes.roommanagement.domain.entities.Civility</class>
<class>com.parisdescartes.roommanagement.domain.entities.EventType</class>
<class>com.parisdescartes.roommanagement.domain.entities.Job</class>
<class>com.parisdescartes.roommanagement.domain.entities.Reservation</class>
<class>com.parisdescartes.roommanagement.domain.entities.Room</class>
<class>com.parisdescartes.roommanagement.domain.entities.RoomType</class>
<class>com.parisdescartes.roommanagement.domain.entities.Tool</class>
<class>com.parisdescartes.roommanagement.domain.entities.User</class>
<class>com.parisdescartes.roommanagement.domain.entities.UserDetail</class>
<class>com.parisdescartes.roommanagement.domain.entities.Schedule</class>
<properties>
<property name="hibernate.connection.autocommit" value="true" />
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="update"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
Did I make a mistake or forgot to specify something ?
Remove the hibernate jar from WEB-INF/lib. JBoss has that bundled, so if you have it on the classpath it probably confuses the classloader.