How can I enhance classes for openJPA with gradle - gradle

I am stuck setting up a simple test program with
- eclipse
- gradle (Eclipse Buildship plugin)
- openjpa
When I try to run my application I get this error, when I call the entityManager.persist(...) method:
Exception in thread "main" org.apache.openjpa.persistence.ArgumentException: Attempt to cast instance "test.jpa.Person#27c6e487" to PersistenceCapable failed. Ensure that it has been enhanced.
FailedObject: test.jpa.Person#27c6e487
The program is simple, it just has one entity (Person.java).
My persistence.xml looks like this:
<?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="ptest"
transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/ptest" />
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver" />
<property name="openjpa.ConnectionUserName" value="..." />
<property name="openjpa.ConnectionPassword" value="..." />
<property name="openjpa.RuntimeUnenhancedClasses" value="unsupported" />
<property name="openjpa.DynamicEnhancementAgent" value="false" />
<property name="openjpa.Log" value="SQL=ERROR" />
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
<property name="openjpa.ConnectionFactoryProperties"
value="PrettyPrint=true, PrettyPrintLineLength=72, PrintParameters=true, MaxActive=10, MaxIdle=5, MinIdle=2, MaxWait=60000" />
</properties>
</persistence-unit>
</persistence>
I have used RuntimeUnenhancedClasses=unsupported and DynamicEnhancementAgent=false, for it was recommended by apache. But I have tried with "supported" and "true" and I still get the same error (for openjpa version 2.1.0 - for higher versions I get a LinageError with "supported/true").
I have tried different versions of jpa, currently using 2.4.2.
I have goodled for gradle scripts to enhance my class, I tried
'at.schmutterer.oss.gradle:gradle-openjpa:0.2.0'
'neva.openjpa'
and some more, but I just cannot enhance the class.
Of course I have goodled for quite some time. I am thankful for any idea I can follow...
Thanks in advance!

Just in case someone else has the same struggle. I have found a solution to this problem:
TomEE ships with JPA and everything that is needed. The eclipse "gradle buildship" plugin can be used to compile code. So the combination of the following components works perfectly fine for me:
apache-tomee-plus-7.0.2
tomcat v8.5 server (pointing to the apache-tommee installation)
Dynamic Web Project (using Dynamic Web Module 3.1)
Java Runtime Environment v1.8
The gradle script reduces simply to this:
apply plugin: 'war'
repositories {
mavenCentral()
}
dependencies {
providedCompile "javax:javaee-api:7.0"
}
I suppose it also works with TomEE 1.7.x and javaee-api:6.0, but I have not tried it.

Related

Quarkus not discovering #Entity entities for persistence.xml based configuration

I'm running into an issue where I can't get Quarkus to work with persistence.xml.
My entities are not discovered so I always get "Not an entity" errors when querying.
(Caused by: java.lang.IllegalArgumentException: Not an entity: ...)
Using Quarkus 1.13.5.Final
This might be related to Quarkus Panache Not Working with persistence.xml after 1.8
I'm migrating an existing app, I only have a single project that contains all the entities. I only use a single persistence unit.
My persistence.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="registry-pu" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.enable_lazy_load_no_trans" value="true"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.jdbc.time_zone" value="UTC"/>
</properties>
</persistence-unit>
</persistence>
I'm only specifying these additional env variables:
QUARKUS_DATASOURCE_USERNAME=
QUARKUS_DATASOURCE_PASSWORD=
QUARKUS_DATASOURCE_JDBC_URL=
I'm also only relying on this persistence.xml because the app being migrated needs the enable_lazy_load_no_trans prop to work
Any help would be appreciated
Found I had to add <class>EntityClass</class> entries to the <persistence-unit> tag for each of my entities to have them included. Otherwise you may be able to try <exclude-unlisted-classes>false</exclude-unlisted-classes>

Infinispan/JDBC as Backend for Hibernate Search on Wildfly/JBoss

I am trying to configure a JDBC-backed Infinispan cache to act as the backend for my Java EE app making use of Hibernate Search. I am deploying on JBoss EAP 7.0 or Wildfly 10. I have a module, cache container, and persistence.xml configuration that does not give me any errors on startup. In addition, I am able to create JPA objects and have them indexed via Hibernate Search as expected. I am then able to query those objects successfully. However, at no time are the SQL tables created in the database that I have configured as my JDBC data source for the cache container. So, obviously, the search indices only exist in memory and are not persisted across app server restarts. Here is what I have done thus far:
Downloaded the Infinispan 8.1.x release that corresponds to the Infinispan release embedded within JBoss EAP. This was done because the hibernate-search modules from Infinispan are not included in the embedded module
I've configured the appropriate modules for the Infinispan hibernate-search module within JBoss EAP
Modified my standalone-full-ha.xml JBoss EAP configuration file to include a JDBC-backed cache-container and cache definitions
Modified my persistence.xml file to make use of an Infinispan cache manager and directory provider
Here is the definition for my cache-container as found in standalone-full-ha.xml:
<cache-container name="hibernateSearch" default-cache="LuceneIndexesData" module="org.infinispan.cachestore.jdbc" jndi-name="java:jboss/infinispan/hibernateSearch">
<transport lock-timeout="60000"/>
<replicated-cache name="LuceneIndexesMetadata" statistics-enabled="true" mode="SYNC">
<binary-keyed-jdbc-store data-source="InfinispanCacheDS" passivation="false" purge="false" shared="true">
<binary-keyed-table>
<id-column name="ID_COLUMN" type="VARCHAR(255)"/>
<data-column name="DATUM" type="BYTEA"/>
</binary-keyed-table>
</binary-keyed-jdbc-store>
</replicated-cache>
<replicated-cache name="LuceneIndexesData" statistics-enabled="true" mode="SYNC">
<binary-keyed-jdbc-store data-source="InfinispanCacheDS" passivation="false" purge="false" shared="true">
<binary-keyed-table>
<id-column name="ID_COLUMN" type="VARCHAR(255)"/>
<data-column name="DATUM" type="BYTEA"/>
</binary-keyed-table>
</binary-keyed-jdbc-store>
</replicated-cache>
<replicated-cache name="LuceneIndexesLocking" statistics-enabled="true" mode="SYNC"/>
</cache-container>
Here is my JDBC data source from standalone-full-ha.xml:
<datasource jndi-name="java:jboss/datasources/InfinispanCacheDS" pool-name="InfinispanCacheDS" enabled="true" use-java-context="true" statistics-enabled="true">
<connection-url>jdbc:postgresql://localhost:5432/db_infinispan_cache</connection-url>
<driver>postgresql-jdbc4</driver>
<pool>
<min-pool-size>10</min-pool-size>
<max-pool-size>20</max-pool-size>
<prefill>true</prefill>
<flush-strategy>IdleConnections</flush-strategy>
</pool>
<security>
<user-name>infinispan_cache</user-name>
<password>mypassword</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker"/>
<validate-on-match>true</validate-on-match>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter"/>
</validation>
<statement>
<track-statements>true</track-statements>
</statement>
</datasource>
Here is my persistence.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<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="MyPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/jdbc/datasources/MyDataSourceDS</jta-data-source>
<shared-cache-mode>ALL</shared-cache-mode>
<properties>
<property name="jboss.entity.manager.factory.jndi.name"
value="java:/MyDataSourceEntityManagerFactory" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.jdbc.batch_size" value="50" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.generate_statistics" value="true" />
<property name="hibernate.connection.release_mode" value="auto" />
<!-- Transactions -->
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />
<property name="hibernate.transaction.flush_before_completion"
value="true" />
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.JBossTransactionManagerLookup" />
<property name="hibernate.max_fetch_depth" value="5" />
<!-- Caching support - Infinispan -->
<property name="hibernate.cache.use_second_level_cache"
value="true" />
<property name="hibernate.cache.infinispan.cachemanager"
value="java:jboss/infinispan/container/hibernate" />
<property name="hibernate.cache.use_second_level_cache"
value="true" />
<!-- Hibernate Search properties - Generic -->
<property name="hibernate.search.reader.strategy" value="shared" />
<property name="hibernate.search.worker.execution" value="sync" />
<property name="hibernate.search.jmx_enabled" value="true" />
<!-- Hibernate Search properties - Infinispan -->
<property name="hibernate.search.infinispan.cachemanager_jndiname"
value="java:jboss/infinispan/hibernateSearch" />
<property name="hibernate.search.default.directory_provider"
value="infinispan" />
<property name="hibernate.search.infinispan.chunk_size"
value="300000000" />
</properties>
</persistence-unit>
</persistence>
When JBoss starts up, I do not see any errors. I also don't see any reference to JDBC, however. I also do not see any errors when persisting JPA objects, so it seems they are being indexed appropriately. It's just that my Hibernate Search index isn't being saved in the database as I would expect.
Can anyone shed some light on what I'm missing here?
As you noticed, the Infinispan extensions used by Hibernate Search for this purpose are not included within the Infinispan module which is part of WildFly / JBoss EAP, so you correctly downloaded the Infinispan modules from the Infinispan project.
What you are missing is that WildFly is able to isolate modules very effectively, so the first thing you have to realise is that you really don't have to match the version of Infinispan as included in WildFly.
Since you will be using the module set from infinispan.org, you should NOT configure these caches in your JBoss EAP configuration file, as the caches defined there are controlled by the clustering subsystem and will affect the cache definitions created by the Infinispan modules included in WildFly (the Infinispan modules at slot "main").
You should include an Infinispan configuration file in your Hibernate Search based application, and have it start a new CacheManager using the right module.
Alternatively you can create another application to start the CacheManager in any way you like - as long as you depend on the right Infinispan modules (avoid the "main" slot) - then register it to JNDI and have Hibernate Search look for that name.
N.B. the Hibernate Search module has a dependency to the optional Infinispan module, so it will attempt to load the right Infinispan module if it's present:
https://github.com/wildfly/wildfly/blob/84d88b8/feature-pack/src/main/resources/modules/system/layers/base/org/hibernate/search/engine/main/module.xml#L53
Be aware also that thanks to the module system, you can override / upgrade the Hibernate Search version.
In terms of versions your restrictions are :
pick an Infinispan module version compatible with your Hibernate Search version of choice
pick an Hibernate Search version compatible with the Hibernate ORM version of choice
(That's right, you can override / upgrade Hibernate ORM as well).
Assuming you are using the default version of Hibernate ORM and Hibernate Search as included in WildFly 10, you could download the Infinispan modules at version 8.2.6.Final (the latest stable release) as it also contains a module
<module name="org.infinispan.hibernate-search.directory-provider" slot="for-hibernatesearch-5.5" >
Or if you're using JBoss EAP, you might prefer to download the JBoss Data Grid distribution, which will contain the same features as the Infinispan modules.

H2 + openJPA + JPA + Spring + apache Camel Java DSL configuration in intellij

I am using IntelliJ IDEA. I have created an embedded H2 database and defined a table in it. My module has the JPA framework support applied. There is no spring.config xml used. I want to do the following - create a persistent H2 database on filesystem and make jpa camel component read from the database.
The persistence.xml looks like this:
<?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="camel" transaction-type="RESOURCE_LOCAL">
<provider>
org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<class>com.workflow2015.database.model.UserEntity</class>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:h2:./dtbase/MYDB;AUTO_SERVER=TRUE;"/>
<property name="openjpa.ConnectionDriverName" value="org.h2.Driver"/>
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=WARN, SQL=WARN, Runtime=WARN"/>
<property name="openjpa.RuntimeUnenhancedClasses" value="supported"/>
<property name="openjpa.ConnectionUserName" value="sa"/>
<property name="openjpa.ConnectionPassword" value=""/>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" />
</properties>
</persistence-unit>
My camel context gets injected with spring-boot, and I am able to run other camel components and EIP. The result that I want to achieve is the following:
from("jpa://UserEntity?consumer.query=select o from UserEntity o")
.process(new Processor() {
#Override
public void process(Exchange exchange) throws Exception {
//do smthing with it
}
});
I have UserEntity autogenerated from my DB schema into POJO. I am using maven for dependencies and the pom file has all the required dependencies.
The issue:
When I run the application for some reason it creates another database("testdb") with unknown schema to me and does not use the information provided in the persistence.xml
2015-06-05 22:35:00,980 [main ] INFO EmbeddedDatabaseFactory Creating embedded database 'testdb'
2015-06-05 22:35:02,138 [main ] INFO tainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'default'
Why is EmbeddedDatabaseFactory creating this phantom database instead of using the one that I have created?
Consequently I cannot use the entity that I have created and error is shown that it cannot find schema.

Tomcat 7 - Spring 3.2.1 - OpenJPA No persistent class is specified in eager initialization mode

I'm trying to get a simple web app called Debugger running under Tomcat 7 using Spring 3.2.1 and OpenJPA. I use Eclipse as the IDE and run Tomcat external to the IDE. I'm getting a error when the WAR is being deployed. This is the error message:
org.apache.openjpa.persistence.ArgumentException: No persistent class is specified in eager initialization mode.
Here is the 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"
version="2.0">
<persistence-unit name="applicationDB" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<properties>
<property name="openjpa.jdbc.SchemaFactory" value="native(ForeignKeys=true)"/>
<property name="openjpa.InitializeEagerly" value="true"/>
<property name="openjpa.DynamicEnhancementAgent" value="false"/>
</properties>
</persistence-unit>
</persistence>
Is the error caused by not having any classes specified in this file? I'm just trying to get a base application configuration setup so I'm not yet ready to place any classes in the persistence file. Maybe you have to have at least one?
Either list your persistent classes, or remove the openjpa.InitializeEagerly property.

javax.persistence.PersistenceException: No Persistence provider for EntityManager named xxx

Can't test a jpa/maven project. I have the error
"javax.persistence.PersistenceException: No Persistence provider for EntityManager named xxx"
when I run "mvn cleant test" from commandline. I have a Java SE project.
I have the persistence confiuration in 'src/test/resources/META-INF/persistence.xml'. And I also has the same for 'src/main/...'.
I can see the persistence.xml in 'target/classes/META-INF'. Only that is from the main, not from the test as I run the tests. This is not yet the problem, since both should work anyway.
Trying the JPA for the first time, but as I see, the files should be ok (location and content).
The persistence unit names should match also.
I'm using Eclipse (EE) with m2 and other necessary plugins, but running maven from commandline. I see no errors in the project.
// Update
Tried fixing the maven build as I noticed it should have the test classes and resources in 'target/test-classes'.
Changed the command to "mvn clean test-compile test"
Now the resources can be found from the correct place, but I still got the same error.
// update
For clarity here's the full persistence.xml
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="xxx"
transaction-type="RESOURCE_LOCAL">
<provider>my.package.EntityManagerFactoryHelper</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/db" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="yyy" />
<property name="javax.persistence.jdbc.password" value="zzz" />
<property name="eclipselink.ddl-generation" value="DROP_AND_CREATE" />
</properties>
</persistence-unit>
I took the helper example from other posts. Basically it just creates the emf using the 'xxx' persistence unit. Here's the helper class. http://pastebin.com/1GE6uMa1
Try to add < provider>org.hibernate.ejb.HibernatePersistence< /provider> inside tag
The provider has to be:
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
Else the table creation fails, even that there is no errors when creating the tables from the entities.
Spent about 2 days resolving this, since I thought I was pointed the right direction by the given hint and didn't check the provider then.
The provider has to be according to the eclipselink, as you could spot it in the configuration of the 'persistence.xml'.
But I failed to say it explicitly. Didn't know it would matter that much and it even succeeded with the table creation once. Not really sure what I had for the provider then, but it failed subsequently (after changes made to the table).
Probably there should had been other configurations by the provider.

Resources