Data Access using Spring MVC, Mysql, JPA not working - java

I am trying to use JPA with Spring MVC using maven. I am new to all three and am stuck at trying to follow the steps in http://www.objectdb.com/tutorial/jpa/eclipse/web..Instead of objectDB, I am using MySQL..
I have added the following dependency to pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
Following is my persistence.xml
<persistence-unit name="GuestbookPU" >
<provider>com.objectdb.jpa.Provider</provider>
<!-- <properties>
<property name="javax.persistence.jdbc.url" value="$objectdb/db/guests.odb"/>
<property name="javax.persistence.jdbc.user" value="admin"/>
<property name="javax.persistence.jdbc.password" value="admin"/>
</properties> -->
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://172.22.201.142:3306/" />
<property name="javax.persistence.jdbc.user" value="tcm_user" />
<property name="javax.persistence.jdbc.password" value="tcm_pwd" />
</properties>
</persistence-unit>
Also, I am using Tomcat and I am not sure if it supports JPA, so I tried tomEE but I am still getting error
java.io.FileNotFoundException: jdbc:mysql:\172.22.201.142:3306 (The filename, directory name, or volume label syntax is incorrect)
The rest of the modules are same as the tutorial, please let me know where I am going wrong.

You seem to want to use MySQL, yet have put the "persistence provider" as ObjectDB. That is a contradiction. ObjectDB as a JPA provider only supports persistence to itself (an Object Database - ODBMS); ask yourself where in the ObjectDB docs did it say it supports persistence to MySQL?.
MySQL is an RDBMS datastore, so if you want to use MySQL then you use a JPA provider that support MySQL, such as DataNucleus JPA for example

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.

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>

Can't connect to H2 database in automatic mixed mode

I have the following config in the persistence.xml of my jpa app:
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:c:/workdir/db/dev;AUTO_SERVER=TRUE"/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="javax.persistence.jdbc.password" value=""/>
<properties>
I suppose it's called 'Automatic Mixed Mode' and i should be able to access it using the jdbc:h2:file:c:/workdir/db/dev url in the h2 console.
I start the h2 console:
java -jar h2-1.3.172.jar -url jdbc:h2:file:c:/workdir/db/dev
Then i see only INFORMATION_SCHEMA and no tables created by my app.
What's wrong ?
Ok, it's because the jpa app is using a different version of h2 jar. So the fix is to use the same versions of h2 jar everywhere.

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

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.

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