Two Persistence Units in persistence.xml - One works, other not - java

I'm having such a strange problem with an JPA application in java. I'm trying to read data from a MySQL database and write it on a ObjectDB embed database but when i try to open the Persistence unit i got this message:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named itunes_puSQL
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at br.com.lagranzotto.itunes.parser.main.iTunesParser.read(iTunesParser.java:78)
at br.com.lagranzotto.itunes.parser.main.iTunesParser.main(iTunesParser.java:72)
My persistence.xml as follows:
<persistence-unit name="itunes_pu" transaction-type="RESOURCE_LOCAL">
<provider>com.objectdb.jpa.Provider</provider>
<class>br.com.lagranzotto.itunes.parser.model.Album</class>
<class>br.com.lagranzotto.itunes.parser.model.Artist</class>
<class>br.com.lagranzotto.itunes.parser.model.Cover</class>
<class>br.com.lagranzotto.itunes.parser.model.Track</class>
<properties>
<property name="javax.persistence.jdbc.url" value="objectdb:itunes.odb"/>
</properties>
</persistence-unit>
<persistence-unit name="itunes_puSQL" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>br.com.lagranzotto.itunes.parser.model.Album</class>
<class>br.com.lagranzotto.itunes.parser.model.Artist</class>
<class>br.com.lagranzotto.itunes.parser.model.Cover</class>
<class>br.com.lagranzotto.itunes.parser.model.Track</class>
<properties>
<property name="eclipselink.jdbc.password" value="**************"/>
<property name="eclipselink.jdbc.user" value="itunes"/>
<property name="eclipselink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="eclipselink.jdbc.url" value="jdbc:mysql://localhost:3306/itunes"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>
I can't have more than one persistence unit per application?

The exception is not related to the first persistence unit or for having more than one persistence unit. Using multiple persistence units in one application is supported and allowed by JPA.
The error message indicates that JPA cannot find a persistence provider (i.e. a JPA implementation) that can handle the itunes_puSQL persistence unit. More specifically, class org.eclipse.persistence.jpa.PersistenceProvider, which is part of EclipseLink (and specified in your XML as the provider) is not found.
As suggested above, check your classpath. Make sure that both EclipseLink and ObjectDB are in the classpath.

Related

Having both JPA and Liquibase: what to do with their configuration files?

My project started with JPA only and it doesn't have Spring. Later, I added Liquibase and I had some issues with the persistence unit name since it is necessary to have one to be able to use EntityManager.
entityManagerFactory = Persistence.createEntityManagerFactory("MyPU");
So, to be able to continue with the tables creation with Liquibase and persisting into the database with JPA, I kept both persistence.xml and liquibase.properties files, despite contaning the same database configuration.
<?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_2.xsd"
version="2.2">
<persistence-unit name="MyPU">
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/jpa_specialist?createDatabaseIfNotExist=true&useTimezone=true&serverTimezone=UTC"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
changeLogFile=src/main/resources/META-INF/database/dbchangelog.xml
url=jdbc:mysql://localhost/jpa_specialist?createDatabaseIfNotExist=true&useTimezone=true&serverTimezone=UTC
username=root
password=root
I've taken a look at liquibase-hibernate and I didn't understand it very well but it seems to be used to generate the diff files, which is not my need at the moment.
Are both files necessary? Is there something I can do to have only one of them?
Liquibase doesn't have a direct way to read the url/username/password information from the presistence.xml file. The liquibase-hibernate extension does add support for diff'ing a database with your java file mapping files, but doesn't change how Liquibase gets the url/username/password.
You said you were not using Spring, but if you are still in a web application, you can use the Liquibase servlet listener to run Liquibase which pulls the connection from a pre-configured datasource. JPA can pull from that same pre-configured datasource instead of re-defining the configuration as well.
Otherwise, unless you want to do a bit of custom Java coding to parse the persistence.xml file and pass it into Liquibase, you do need both files.
To avoid the repetition you could do something like defining build properties in your maven/gradle/whatever setup and have <property name="javax.persistence.jdbc.url" value="${database.url}"/>in your persistence.xml source file, and url: ${database.url} in your liquibase.properties file.

Can't connect to JDBC database

I am trying to run a project in my WAS local server. The problem is I keep getting this error:
Unsupported use of GenericConnection. A GenericConnection is provided
during application start when creating an EntityManagerFactory for a
persistence unit which has configured one of its datasource to be in
the component naming context; java:comp/env. During application start,
the component naming context will not exist, and the correct
datasource cannot be determined. When the persistence unit is used,
the proper datasource and connection will be obtained and used.
In my persistence.xml file I have this:
<persistence-unit name="myPUnit" transaction-type="RESOURCE_LOCAL" >
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<non-jta-data-source>jdbc/myPUnit</non-jta-data-source>
<properties>
<property name="eclipselink.logging.level" value="INFO" />
<property name="eclipselink.jdbc.driver" value="oracle.jdbc.OracleDriver" />
<property name="eclipselink.cache.shared.default" value="false"/>
</properties>
</persistence-unit>
I don't think there's anything wrong with my persistence.xml file, but I keep getting the above mentioned error.
I'm using java 1.8, RAD 9.5 and WAS 8.5.

Create a Derby DB in Glassfish and connect to Java EE application

I'm trying to write a Java EE application that uses JPA to access a database. Until now I just used the #Entity annotation and left everything else to the default state (for example the persistence.xml file was using _TimerPool as the jta-data-source, and I didn't create any db).
So I wanted to try and use an actual database. I went into the Services screen, JavaBD > Create new database, set it up with a name and password.
The DB's url: jdbc:derby://localhost:1527/Prova
Then I created the persistence.xml file for my application through Glassfish's wizard:
<persistence-unit name="JobsPU" transaction-type="JTA">
<jta-data-source>java:app/Prova</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="javax.persistence.schema-generation.database.target" value="database-and-scripts"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/Prova"/>
<property name="javax.persistence.jdbc.user" value="paolo"/>
<property name="javax.persistence.jdbc.password" value="paolo"/>
</properties>
</persistence-unit>
And when I try to deploy I get this exception:
Grave: Exception while preparing the app : Invalid resource : { ResourceInfo : (jndiName=java:app/Prova__pm), (applicationName=Jobs) }
com.sun.appserv.connectors.internal.api.ConnectorRuntimeException: Invalid resource : { ResourceInfo : (jndiName=java:app/Prova__pm), (applicationName=Jobs) }
Seems to be related to the JNDI naming. Of what I honestly don't know, I'm still trying to learn. If I go to Glassfish's console, under the JNDI listing I can't see anything that seems to be related to my database (not in JDBC Connection Pools nor in JDBC Resources). What should I do?
Thank you very much in advance for any help.
To configure PersistenceUnit for EE container you have to use jta-data-source and transaction-type="JTA". For jta-data-source you have to specify JNDI name of JDBC ConnectionPool which have to be configured in EE container (glassfish server in your case). There is tutorial How to set up a JDBC Connection Pool on Glassfish. Also in this case PersistenceUnit properties like
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/Prova"/>
<property name="javax.persistence.jdbc.user" value="paolo"/>
<property name="javax.persistence.jdbc.password" value="paolo"/>
will be ignored for EE container (see this documentation).
To use this properties you can configure PersistenceUnit for SE environment. Before this i recommend you to read article differences between RESOURCE_LOCAL and JTA persistence contexts.
Configuration for SE application will be look like:
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<provider>
org.eclipse.persistence.jpa.PersistenceProvider
</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/Prova"/>
<property name="javax.persistence.jdbc.user" value="paolo"/>
<property name="javax.persistence.jdbc.password" value="paolo"/>
</properties>
</persistence-unit>

JPA specify multiple sql-load-script-source

I need to fill my database on ear startup, so I add javax.persistence.sql-load-script-source tag with reference to the file.
<persistence-unit name="MyPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:/jboss/datasource/mydatasource</jta-data-source>
<jar-file>Entities.jar</jar-file>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.DefaultSessionLog"/>
<property name="eclipselink.logging.level" value="ALL"/>
<property name="eclipselink.logging.level.sql" value="ALL"/>
<property name="javax.persistence.sql-load-script-source" value="META-INF/defaultdata.sql"/>
<property name="javax.persistence.sql-load-script-source" value="META-INF/insertnations.sql"/>
</properties>
</persistence-unit>
It works, but now i want to use two file because i want to separate "most static" data (like nations and regions of the world) from "per-deploy" data (like some configurations).
Using the tag twice not works.
Is there something wrong?
It is possible to do what i want?
The persistence unit is used in a EAR. I use Wildfly and eclipselink 2.5.2 as persistence provider

hibernate reverse engineering persistence unit not found

I havea java maven3 project in Eclipse IDE with jboss tools installed. I'm using hibenrate4
I'm triing to setup the hibenrate configuratoin in hibernate view to test hql queries, the problem is that is says that it couldnt find the persistence unit. I have the persistence.xml placed in src/main/resources/META-INF/persistence.xml
is there some maven config that I nead to set?
Persistence.xml
<persistence-unit name="ypay">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/ypay</jta-data-source>
<properties>
<!-- Properties for Hibernate -->
<!-- <property name="hibernate.hbm2ddl.auto" value="validate" /> -->
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
</properties>
hibernate.properties
hibernate.connection.password=
hibernate.connection.username=root
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.connection.url=jdbc:mysql://****************:3306/ypay?useUnicode=true&characterEncoding=UTF-8
hibernate.connection.provider_class=org.hibernate.connection.DriverManagerConnectionProvider
hibernate.datasource=
hibernate.transaction.manager_lookup_class=
You need a META-INF/persistence.xml with
<persistence-unit name="ypay">
(usually put it in WEB-INF/classes/META-INF. When using maven you can try placing it in src/main/resources/META-INF)
Please refer to this doc for details.

Categories

Resources