OpenJPA does not create DB2 tables when deploying on Websphere Application Server - db2

I deployed my ear file on Websphere Application server V7.0 and start the application. However, it does not auto create table to my database DB2 and don't have any error message.
Please see my persitence.xml file
<?xml version="1.0" encoding="UTF-8" ?>
<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"
version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="xcrm_ejb" >
<jta-data-source>jdbc/xcrm</jta-data-source>
<non-jta-data-source>jdbc/non_xcrm</non-jta-data-source>
<mapping-file>META-INF/orm.xml</mapping-file>
<class>ch.xpertline.xcrm.entity.base.BaseEntity</class>
<class>ch.xpertline.xcrm.entity.Address</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="openjpa.Log" value="DefaultLevel=INFO,SQL=TRACE,File=./dist/jpaEnhancerLog.log,Runtime=INFO,Tool=INFO"/>
<property name="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=72"/>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
</properties>
</persistence-unit>
</persistence>
My orm.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="1.0" 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">
<persistence-unit-metadata>
<persistence-unit-defaults>
<schema>soreco</schema>
</persistence-unit-defaults>
</persistence-unit-metadata>
</entity-mappings>
Thanks

Try sending in an application request. I don't think synchronize mappings is triggered until the first EntityManager is created.

Related

Wildfly datasource marked as JTA enabled fails to auto-create schema

I've got a JPA unit that fails to create the database schema in Wildfly10 when the data-source is marked with jta=true. Any idea how could I work around this problem?
The stack trace:
javax.persistence.PersistenceException: Could not set provided connection [org.jboss.jca.adapters.jdbc.jdk7.WrappedConnectionJDK7#12f9ae77] to auto-commit mode (needed for schema generation)
at org.hibernate.jpa.internal.schemagen.JpaSchemaGenerator$ConnectionProviderJdbcConnectionAccess.<init>(JpaSchemaGenerator.java:644)
at org.hibernate.jpa.internal.schemagen.JpaSchemaGenerator$ConnectionProviderJdbcConnectionAccess.<init>(JpaSchemaGenerator.java:620)
at org.hibernate.jpa.internal.schemagen.JpaSchemaGenerator.determineAppropriateJdbcConnectionContext(JpaSchemaGenerator.java:402)
at org.hibernate.jpa.internal.schemagen.JpaSchemaGenerator.access$100(JpaSchemaGenerator.java:55)
at org.hibernate.jpa.internal.schemagen.JpaSchemaGenerator$GenerationProcess.execute(JpaSchemaGenerator.java:113)
at org.hibernate.jpa.internal.schemagen.JpaSchemaGenerator.performGeneration(JpaSchemaGenerator.java:65)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:885)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
..
Wildfly data source:
<datasource jta="true" jndi-name="java:/xxx" pool-name="xxx" enabled="true" use-ccm="true">
<connection-url>jdbc:mariadb:failover://xxx/xxx</connection-url>
<driver-class>org.mariadb.jdbc.Driver</driver-class>
<driver>mariadb-java-client-1.5.8.jar</driver>
..
</datasource>
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="xxx" transaction-type="JTA">
<jta-data-source>java:/xxx</jta-data-source>
<class>xxx</class>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create" />
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />
</properties>
</persistence-unit>
</persistence>

No transaction is currently active under the eclipselink and jboss

Server: Jboss EAP6.2
JPA: eclipselink 2.4.*
transaction-type="JTA"
get error message:
Exception Description: No transaction is currently active
...
at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:189)...
...[some ejb]$$view[some number].[some method](Unknow Source)
...
You will need add in the configuration file persistence.xml the next entry
<property name="eclipselink.target-server" value="JBoss" />
Example complete:
<?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="SQLServer" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/SQLServer</jta-data-source>
<properties>
<property name="eclipselink.target-server" value="JBoss" />
</properties>
</persistence-unit>
</persistence>
<property name="eclipselink.target-server" value="JBoss" />
this config can solved that error

Error No Script Specified when using JPA Persistence Schema Generation

Running on Wildly 8.2.0.Final. I get the following error when trying to generate a database schema from a script.
Caused by: javax.persistence.PersistenceException: Schema generation configuration indicated to include CREATE scripts, but no script was specified
<?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_mavenFlowChartDB5_war_1.0-SNAPSHOTPU" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/postgresql</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name=""/>
<property name="javax.persistence.schema-generation.create-source" value="script"/>
<property name="javax.persistence.schema-generation.drop-source" value="script"/>
<property name="javax.persistence.schema-generation.scripts.action" value="none"/>
<property name="javax.persistence.schema-generation.scripts.drop-target" value="META-INF/drop-script.sql"/>
<property name="javax.persistence.schema-generation.scripts.create-target" value="META-INF/create-script.sql"/>
</properties>
</persistence-unit>
</persistence>
Where the create script is located:
/src/main/resources/META-INF/create-script.sql
Thanks in advance!
The Wildfly persistence schema generator gui creates incorrect property names for the drop and create targets.
The correct property names are
<property name="javax.persistence.schema-generation.drop-script-source" value="META-INF/drop-script.sql"/>
<property name="javax.persistence.schema-generation.create-script-source" value="META-INF/create-script.sql"/>
change create-script to create-script-source and drop-script to drop-script-source
source: https://github.com/hantsy/ee7-sandbox

Tomee with Arquillian using Postgres DB user lacks privilege or object not found

I have a problem when running arquillian tests against postgres db using tomee.
With all the info on the web I'm still struggling to get the problem solved.
javax.ejb.EJBException: The bean encountered a non-application exception; nested exception is:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: CREDENTIALS
Error Code: -5501
Call: SELECT ID, PASSWORD, USERNAME FROM credentials WHERE (USERNAME = ?)
bind => [phil]
The DB:
Name: registry
Table: credentials
Sits under manually created Schema: postgres
persistence.xml under directory src/main/resources
<?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="registry" transaction-type="JTA">
<jta-data-source>RegistryDS</jta-data-source>
<non-jta-data-source>UnmanagedRegistryDS</non-jta-data-source>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>za.co.registry.client.login.Credentials</class>
<properties>
<property name="eclipselink.debug" value="OFF"/>
<property name="eclipselink.weaving" value="static"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="eclipselink.logging.logger" value="DefaultLogger"/>
</properties>
</persistence-unit>
</persistence>
tomee.xml
<Resource id="RegistryDS" type="DataSource">
jdbcDriver=org.postgresql.Driver
jdbcUrl=jdbc:postgresql://127.0.0.1:5432/registry
userName=postgres
password=postgres
JtaManaged=true
</Resource>
<Resource id="UnmanagedRegistryDS" type="DataSource">
jdbcDriver=org.postgresql.Driver
jdbcUrl=jdbc:postgresql://127.0.0.1:5432/registry
userName=postgres
password=postgres
JtaManaged=false
</Resource>
pom.xml extract for arquillian tests
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>arquillian-tomee-embedded</artifactId>
<version>1.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<version>1.0.3.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
arquillian.xml extract
<container qualifier="tomee" default="true">
<configuration>
<property name="httpPort">-1</property>
<property name="stopPort">-1</property>
<property name="dir">target/apache-tomee-remote</property>
<property name="appWorkingDir">target/arquillian-test-working-dir</property>
<property name="properties" />
</configuration>
</container>
The ServiceTest.java file when loading the resources.
#Deployment
public static WebArchive createDeployment() {
WebArchive webArchive = newArchive();
webArchive.addClasses(Credentials.class);
webArchive.addAsResource("META-INF/persistence.xml");
webArchive.addAsResource("META-INF/beans.xml");
return webArchive;
}
And last the test findCredentialsByUsernameTest method in ServiceTest.java
#Test
public void findCredentialsByUsernameTest() {
Credentials login = LoginService.findByUsername("phil");
Assert.assertNotNull(login);
}
I do not start or end any EntityTransaction's in the test class.
It works injecting a EJB when the DB call in the service is removed.
What am I missing in the config or doing wrong for this not to be working?
Ok I think I know what I did wrong.
I need to run the tests against a embedded db.
The steps that I followed to get Arquillian tests to work against a embedded db;
Removed un-managed data source from tomee.xml, I added it because I wanted to use it for my tests. The other data source is still there because it is used when deploying to tomee to connect to postgres db.
<Resource id="UnmanagedRegistryDS" type="DataSource">
jdbcDriver=org.postgresql.Driver
jdbcUrl=jdbc:postgresql://127.0.0.1:5432/registry
userName=postgres
password=postgres
JtaManaged=false
</Resource>
I'm going to connect to HSQLDB.
HSQLDB (HyperSQL DataBase) is the leading SQL relational database software written in Java. It offers a small, fast multithreaded and transactional database engine with in-memory and disk-based tables and supports embedded and server modes
Next I create a second persistence.xml in src/test/resources called test-persistence.xml. Keep in mind the persistence-unit name testDatabase it is going to be used in the arquillian.xml file
<?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="test" transaction-type="JTA">
<jta-data-source>testDatabase</jta-data-source>
<class>za.co.registry.client.login.Credentials</class>
<properties>
<property name="openejb.jpa.init-entitymanager" value="true" />
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
</properties>
</persistence-unit>
</persistence>
The arquillian.xml, I'm setting the testDatabase persistence unit properties in the file. See below under properties.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<arquillian
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
<container qualifier="openejb-embedded" default="true">
<configuration>
<property name="httpPort">-1</property>
<property name="stopPort">-1</property>
<property name="dir">target/apache-tomee-remote</property>
<property name="appWorkingDir">target/arquillian-test-working-dir</property>
<property name="properties">
testDatabase = new://Resource?type=DataSource
testDatabase.JdbcUrl = jdbc:hsqldb:mem:my-datasource
</property>
</configuration>
</container>
</arquillian>
Configuring my ServiceTest.java file to include the new persistence.xml file.
webArchive.addAsWebInfResource("META-INF/test-persistence.xml", "persistence.xml");
webArchive.addAsResource("META-INF/beans.xml");
Now I can run the findCredentialsByUsernameTest method.
#Test
public void findCredentialsByUsernameTest() {
//create credentials first
Credentials newCredentials = loginService.newCredentials(new Credentials("john", "password"));
//search for credentails
Credentials login = loginService.findByUsername("john");
Assert.assertNotNull(login);
Assert.assertEquals(newCredentials.getUsername(), login.getUsername());
}

JPA- No Persistence provider for EntityManager for MSAccess DB

Im trying to use JPA to connect to a MS access DB using a JDBC:ODBC connection. Here is my Persistance.xml code :
<?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="DSRJPA">
<provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
<class>com.entity.AccessEntity</class>
<properties>
<property name="toplink.jdbc.url" value="jdbc:odbc:MSAccessDB"/>
<property name="toplink.jdbc.user" value="admin"/>
<property name="toplink.jdbc.driver" value="sun.jdbc.odbc.JdbcOdbcDriver"/>
<property name="toplink.jdbc.password" value="admin"/>
<property name="toplink.jdbc.read-connections.min" value="1"/>
<property name="toplink.jdbc.write-connections.min" value="1"/>
</properties>
</persistence-unit>
</persistence>
But when i run the code, i get this error :
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named DSRJPA: The following providers:
oracle.toplink.essentials.PersistenceProvider
oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider
Returned null to createEntityManagerFactory.
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:154)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
at com.test.Access.main(Access.java:19)
Please throw me some light on this error.
Thanks in Advance.
Have you referenced your persistence unit in the class?
i.e.
EntityManagerFactory emf = Persistence.createEntityManagerFactory("DSRJPA");