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

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.

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>

Settng the schema of a Hibernate entity extra to the default schema configuration

I have a Hibernate / Spring 4 application, where the default schema of the entity classes is already configured, e.g. they end up mapped as testschema.table or prodschema.table.
However there is another entity class which needs its own configurable schema, e.g. only this entity must be mapped to testschema2.anothertable or prodschema2.anothertable.
Nice would be something like this:
#Entity
#Table(name="anothertable", schema = "${db.AntherEntitySchema}")
public class AnotherEntity {
// ..
}
where the schema gets injected from the properties file, but such a feature seems only to work with the #Value annotation.
Any idea how to proceed?
We solved it by this applicationContextPersistence.ctx.xml
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceXmlLocation"
value="classpath:META-INF/persistence-${env}.xml" />
<!-- .. -->
</bean>
where Spring can substitute the ${} expression from a property file.
We then have a persistence-dev.xml etc which features a specific orm-dev.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="fooUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<mapping-file>META-INF/orm-dev.xml</mapping-file>
</persistence-unit>
<!-- .. -->
</persistence>
The orm-dev.xml is now responsible for the entity mapping:
<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
version="1.0">
<description> XML Mapping file</description>
<package>foo.server.model</package>
<entity class="AnotherEntity">
<table schema="testschema2" name="anothertable" />
<attributes>
<!-- .. -->
</attributes>
</entity>
</entity-mappings>
Finally we removed the mapping annotations from the AnotherEntity POJO, as this one is now mapped via the orm-dev.xml file. The other enitity classes kept their annotations.
Note: We use the Spring Tool Suite flavour of Eclipse. This IDE expects a persistence.xml, so to get rid of an error message we kept a minimal persistence.xml to not have to remember the IDE option which deactivates the validator.

Transaction is not committed when using local/global JNDI datasource lookup in a spring/eclipselink project in weblogic

When I use the global JNDI name in the persistence.xml, everything works ok.
It goes wrong when I use a local JNDI name, add the resource-ref to the web.xml and map the local name to the global name in the weblogic.xml. He deploys successfully, finds the datasource, does selects and inserts, but never commits! The commit does happen when I use the global JNDI name directly in the persistence.xml.
My spring context is as follows:
<bean id="localContainerEntityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="rctUnit" />
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.WebLogicJtaTransactionManager" />
<tx:annotation-driven transaction-manager="transactionManager" />
The persistence.xml after the change:
<persistence-unit name="rctUnit" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:comp/env/jdbc/rct</jta-data-source>
And the following added to the web.xml and the weblogic.xml:
<resource-ref>
<description>RCT DB</description>
<res-ref-name>jdbc/rct</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-description>
<res-ref-name>jdbc/rct</res-ref-name>
<jndi-name>db.datasource.rct</jndi-name>
</resource-description>
Following versions are used:
eclipselink: 2.0.2
jpa: 1.0.0
spring: 3.2.0.RELEASE
weblogic: 10.3.3
I was able to solve it by adding an eclipseLink property to the persistence.xml:
<property name="eclipselink.target-server" value="weblogic" />
I don't have any clue why this property is not needed when the global JNDI name is used in the persistence.xml. The eclipse property is not needed then to make it work. When using the local JNDI name in the persistence.xml, this property is needed to make the transaction commits.
Probably because the prefix java:comp/ is different according to deployment environment, some may not use the "comp" part

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.

Spring DB2 JPA Entity Manager Problem

I'm trying to configure Spring, JPA and DB2 in order to have the entity manager instance to be used in my spring controllers but according how I have configured Spring this not happens.
These are the two attempts of configuration of spring:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource" />
<bean name="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="em" />
</bean>
<bean id="em"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="fileUtility" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
<property name="database" value="DB2" />
<property name="showSql" value="true" />
</bean>
</property>
</bean>
the second is this:
<!-- Entity manager factory bean. -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="Sample" />
</bean>
<!-- Entity manager bean. -->
<bean id="em" factory-bean="entityManagerFactory"
factory-method="createEntityManager" />
and the entity manager is injected in this way:
<bean id="messageService" class="utilities.services.impl.MessageServiceImpl">
<property name="entityManager" ref="em" />
</bean>
but I have always this exception:
Caused by: java.lang.IllegalArgumentException: methods with same signature createEntityManager() but incompatible return types: [interface com.ibm.websphere.persistence.WsJpaEntityManager, interface org.apache.openjpa.persistence.OpenJPAEntityManagerSPI]
I don't know how can be fixed. Has anyone encountered this problem?
Thanks in advance.
[EDIT]
This is my persistence.xml:
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
<persistence-unit name="fileUtility"
transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<mapping-file>META-INF/mapping.xml</mapping-file>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:db2://localhost:50000/db2admin" />
<property name="openjpa.ConnectionDriverName" value="COM.ibm.db2.jdbc.app.DB2Driver" />
<property name="openjpa.ConnectionUserName" value="db2admin" />
<property name="openjpa.ConnectionPassword" value="XXXX" />
<property name="openjpa.FlushBeforeQueries" value="true"/>
<property name="openjpa.RuntimeUnenhancedClasses" value="supported" />
</properties>
</persistence-unit>
<persistence-unit name="fileUtility2" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>file_ds</jta-data-source>
<mapping-file>META-INF/mapping.xml</mapping-file>
<properties>
<property name="openjpa.Log" value="SQL=TRACE"/>
<property name="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=72"/>
</properties>
</persistence-unit>
</persistence>
WebSphere has a JPA implementation bundled. So no need to add openjpa to your lib. In fact, WebSphere is using OpenJPA, so you are not losing anything. Look here for more details
When using a jda-data-source, you need to have transaction-type="JTA". Also, you should not specify connection properties - they are specified in the datasource.
And get rid of the <provider> - the document I linked says:
If no JPA provider is configured in the element of the persistence.xml file within an EJB module, the default JPA provider that is currently configured for this server is used
I believe you're doing the wrong configuration, because you're configuring it "à la Tomcat". If you're using a Java EE application server, such as WAS, you should:
In Spring application context xml file
configure the DAO bean by a <bean> definition
configure the JNDI definition for the datasource created in the application server via a
<jee:jndi-lookup>
definition; the
name
attribute should be
persistence/XXX, where XXX shall match the
<persistence-unit name="XXX" transaction-type="JTA">
in persistence.xml file
The id attribute in the
<jee:jndi-lookup id=YYY> should point to the
name=YYY parameter of the Entity Manager definition in the DAO, this is to say,
#PersistenceContext(name=YYY) EntityManager em;
Specify
<tx:annotation-driven /> and
<tx:jta-transaction-manager />
In file
web.xml of your web app you should include a definition using the xml tag
<persistence-unit-ref> whose
<persistence-unit-ref-name> parameter shall be the
persistence/XXX JNDI name as specified in persistence.xml (shown above).
Finally, you should create a JNDI definition in the application server (AS dependant) that defines the JNDI name for the JDBC connection. This name should match the
<jta-data-source> xml tag in the persistence.xml file, and it is the only link between the JPA definition and the JDBC defined in the application server.
To round up:
Application Context Spring file
<bean class="DAO implementation class" />
<jee:jndi-lookup id="YYY" jndi-name="persistence/XXX" />
<tx:annotation-driven />
<tx:jta-transaction-manager />
persistence.xml file
<persistence-unit name="XXX" transaction-type="JTA">
<jta-data-source>jdbc/DSN</jta-data-source>
</persistence-unit>
web.xml file
...
<persistence-unit-ref>
<persistence-unit-ref-name>persistence/XXX</persistence-unit-ref-name>
</persistence-unit-ref>
...
DAO (only #PersistenceContext shown)
...
#PersistenceContext(name = "YYY")
EntityManager em;
...
Application Server: jdbc/DSN links to the connection definition, where the driver for the DBM is. Depends on both the AS and the DBM used.
Thus, you may see the connection between the DAO -> Spring Application Context file -> persistence.xml and web.xml files -> Application Server JNDI names. IF you're using a full Java EE application server (such as WAS, Weblogic or GlassFish) you don't have to use Spring interface modules; only defnitions in the app server (see Spring documentation, section 12.6.3).

Resources