Database creation not working in eclipselink with glassfish 4 and Postgres - postgresql

I want to create the database based on Entities. Configuration:
Glassfish: GlassFish Server Open Source Edition 4.1 (build 13)
Eclipselink: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd (delivered with glassfish)
Database: PostgreSQL Version: 9.4.2
Driver: PostgreSQL Native Driver Version: PostgreSQL 9.4 JDBC4.1 (build 1201)
From the moment eclipselink starts creating the database I see the following statements in the logs after putting a lot of log parameters on finest:
SELECT ID FROM table_name WHERE ID <> ID
SELECT 1
This is repeated 4-5 times. The first query gives the following obvious postgres error:
org.postgresql.util.PSQLException: ERROR: relation "table_name" does not exist
Position: 16
As a result a following queries give the following error:
org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block
After this eclipselink continues creating the tables:
CREATE TABLE table_name (ID BIGINT NOT NULL, PRIMARY KEY (ID))
But produces the same error.
For some reason creating the database happens in a transaction. I found the source code responsible, but I can't find out how the transaction is started. I can give more detailed information, but maybe somebody can already help now.
Edit: The persistence.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="h ttp://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="AntennasOperatingSystemServerPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<non-jta-data-source>MyDatabase</non-jta-data-source>
<class>... bunch of classes ... </class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.level" value="ALL"/>
<property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.DefaultSessionLog"/>
<property name="eclipselink.deploy-on-startup" value="true"/>
<property name="eclipselink.ddl-generation.output-mode" value="database"/>
<property name="eclipselink.logging.level.sql" value="FINEST"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.target-database" value="PostgreSQL" />
</properties>
</persistence-unit>
</persistence>

Have you tried setting the eclipselink.target-database property to PostgreSQL or org.eclipse.persistence.platform.database.PostgreSQLPlatform?
Please share your persistence.xml file if the above does not resolve the issue.

I did something similar some time ago. The following configuration work well.
<persistence-unit name="EjemploJpaPU" 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://localhost:1527/example"/>
<property name="javax.persistence.jdbc.driver"
value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="123"/>
<property name="eclipselink.logging.level" value="ALL"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
Obviously, once the tables are created, it is necessary to change the value of the property eclipselink.ddl-generation to none.
NOTE: The database was Derby, but I've also tried it with MySQL. I think that it will work with PostgreSQL.

Related

OpenJPA Exception related to JDBC Driver and connection URL in TomEE+

We are facing issue in our application with respect to the persistence configuration. The application runs on TomEE Plus (7.0) and uses Open JPA implementation. Below is the error which we are getting
<openjpa-2.4.2-r422266:1777108 nonfatal general error> org.apache.openjpa.persistence.PersistenceException: There were errors initializing your configuration: <openjpa-2.4.2-r422266:1777108 fatal user error> org.apache.openjpa.util.UserException: A connection could not be obtained for driver class "oracle.jdbc.xa.client.OracleXADataSource" and URL "jdbc:oracle:thin:#//mydburl.abc.com:1881/MYSID.ABC.COM". You may have specified an invalid URL.
We dont' get the above error when using oracle.jdbc.driver.OracleDriver. The XA driver settings were working fine on IBM WebSphere 8.5. Is there anything else which we need to configure in TomEE+ to get it working?
Update
Adding the persistence.xml file as well
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0">
<persistence-unit name="MySchema" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>myjndi</jta-data-source>
<class>com.abc.jpa.entity.Tuser</class>
<class>com.abc.jpa.entity.Taddress</class>
<properties>
<property name="tomee.jpa.factory.lazy" value="true"></property>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.xa.client.OracleXADataSource" />
<property name="javax.persistence.jdbc.url"
value="jdbc:oracle:thin:#//mydburl.abc.com:1881/MYSID.ABC.COM" />
<property name="javax.persistence.jdbc.user" value="userName" />
<property name="javax.persistence.jdbc.password" value="password" />
</properties>
</persistence-unit>
</persistence>
As mentioned, it works fine if I use oracle.jdbc.driver.OracleDriver as the jdbc driver class

Integrate BoneCP and EclipseLink with LOCAL_RESOURCE transaction-type

I am setting up a minimal working example where I use EclipseLink in a Java EE (JSF) project. My example works perfectly with the following configuration and code:
persistence.xml
<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="issat_PU_Local" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>pro.entity.Utilisateur</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/issat" />
<property name="javax.persistence.jdbc.user" value="java" />
<property name="javax.persistence.jdbc.password" value="JaVa" />
<property name="eclipselink.logging.level.sql" value="FINE" />
<property name="eclipselink.logging.parameters" value="true"/>
</properties>
</persistence-unit>
</persistence>
Since I don't want to use JTA I am using RESOURCE_LOCAL as transaction type and perfectly leaving with "manually" handling transaction inside my DAO.
I now want to use BoneCP connection pool with my example.
How should I precisely modify my persistence.xml to do that? I found many configuration examples in the internet using also Hibernate, but I want to stay with EclipseLink.
I couldn't do it with bonecp, but I could get a pool connection with C3PO.
I followed this link : http://javabeginnerstutorial.com/hibernate/connection-pooling-with-hibernate-4/
I copied the dependency and the properties but, instead of <property name="hibernate.c3p0.min_size">1</property> put the value in the tag like this<property name="hibernate.c3p0.min_size" value="1"/>, otherwise I got an error.
Finally mi persitence.xml look this way:

Access to Derby database from another project using EclipseLink

I have a dynamic web project that creates and writes Derby database. Everything works fine. Here is the persistence.xml I used:
<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" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="alerts" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>test.Alert</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:derby:databases/bDb;create=true" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="user-pwd" />
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
<property name="eclipselink.logging.level" value="FINEST" />
<property name="eclipselink.logging.level.sql" value="FINEST" />
</properties>
</persistence-unit>
Now I would like to create a java project that reads the same database. So I use the same persistence.xml (except that I change "create=true" to "create=false" since I don't want to create a new table). but I got this error:
[EL Severe]: ejb: 2014-12-28 22:27:20.379--ServerSession(1488953836)--Thread(Thread[DefaultQuartzScheduler_Worker-7,5,main])--Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Database 'databases/bDb' not found.
Error Code: 40000
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:331)
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:326)
at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:138)
at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.setOrDetectDatasource(DatabaseSessionImpl.java:204)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:741)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:239)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:685)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:204)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:304)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:336)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:302)
If I use "create=true", it will create a new database instance (it is different from the one I created from the web project). How do I fix this problem? Thanks a lot!
Since you're using a relative filename in your jdbc.url (databases/bDb), Derby is looking for the database relative to your application start directory.
And your two separate applications have two separate directories in which they are being run.
You could address this in a number of ways:
Arrange for each application to be run from the same directory
Use an absolute database path name, not a relative one, in your jdb.url value
Use the Derby Network Server configuration, not the Derby Embedded configuration
And there are undoubtedly other approaches.

Persistence.xml for JBoss AS 7.1.1

I have a client and server setup to run with JBoss AS 7.1.1 and I have a question about my persistence.xml. Currently it looks like this:
<persistence>
<persistence-unit name="GrahamsProj">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/WorkCenterDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
</properties>
</persistence-unit>
</persistence>
I have persistence.xml in the META-INF folder of my server. I must be missing some properties though because I'm getting this error when I try to query the database I'm connected to.
11:35:07,840 ERROR [org.jboss.ejb3.invocation] (EJB default - 1) JBAS014134:
EJB Invocation failed on component GrahamsProjBean for method public abstract
void grahamsprojserver.session.interfaces.GrahamsProjBeanRemote.test(): javax.ejb.EJB
Exception: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException:
Schemas is not mapped [FROM Schemas s]
Is there something I'm missing?
you need to include all your model classes in persistence.xml. example:
<persistence>
<persistence-unit name="GrahamsProj">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/WorkCenterDS</jta-data-source>
<class>your.class.model.Example</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
</properties>
</persistence-unit>
</persistence>
You would probably need to update the hibernate.dialect property too. In your example you are using "org.hibernate.dialect.HSQLDialect" and you use Oracle drivers, e.g. "oracle.jdbc.OracleDriver". If you are using Oracle database, you need to update hibernate dialect, e.g. "org.hibernate.dialect.Oracle10gDialect".
Here's your updated code:
<persistence>
<persistence-unit name="GrahamsProj">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/WorkCenterDS</jta-data-source>
<class>your.class.model.Example</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
</properties>
</persistence-unit>
</persistence>

Rewriting Derby DB tables (with FK) via JPA

I can fill 2 tables in a DB where the tables have mappings(one table has a FK from he other). The
EclipseLink DataSource show the flush to the DB has been done ok.
I am trying to rewrite the tables (with same data a second time ) while the persistence.xml has
the properties clause as follows :
<property name="eclipselink.ddl-generation" value="drop-create-tables" />
yet I see no dropping (erase) of the previous tables before the rewriting and therefore get rejected with the following exception message about a duplicate key :
"Internal Exception:
java.sql.SQLIntegrityConstraintViolationException: The statement was
aborted because it would have caused a duplicate key value in a unique
or primary key constraint or unique index identified by
'SQL120206135740510' defined on 'HOST'. " ( HOST is the table pointed
to by the FK).
What am I missing in the persistence.xml or else that should make the table drop before the rewrite ?
Below see the persistence.xml file ( the DB file is called 'test' . The schema is name after the user name (APP in my case) ):
<?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="jpa_test">
<class>Host</class>
<class>Vm</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://im6-64s:1527/test;create=true" />
<property name="javax.persistence.jdbc.user" value="APP" />
<property name="javax.persistence.jdbc.password" value="passw0rd" />
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="drop-create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
</properties>
</persistence-unit>
</persistence>
"drop-create-tables" should be "drop-and-create-tables"
Try adding the "eclipselink.logging.level" property with a value of Finest or ALL to see why the drop might not be being executed.