Unrecognized JPA persistence.xml XSD version : `` - hibernate, java ee and postgresql - postgresql

The issue is appearing when trying to make entityManager with entitiManagerFactory.
Application is running inside docker container and postgresql database is on localhost of the machine (not inside docker).
my persistence.xml
<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_1.xsd"
version="2.1">
<!-- Define persistence unit -->
<persistence-unit name="mypersistenceunit">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>some.path.SimplifiedUserGroup</class>
<class>some.path.UserSettings</class>
<class>some.path.UserGroupSettings</class>
<class>some.path.UserGroup</class>
<class>some.path.AppUser</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/localdatabase" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="postgres" />
</properties>
</persistence-unit>
and the repository class:
public List<SimplifiedUserGroup> findAll() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("mypersistenceunit");
entityManager = emf.createEntityManager();
return entityManager.createNamedQuery("UserGroup.findAll", SimplifiedUserGroup.class).getResultList();
}
there is an error:
javax.persistence.PersistenceException: Unable to locate persistence units
and then:
java.lang.IllegalArgumentException: Unrecognized JPA persistence.xml XSD version : ``
I tried several tutorials and read stackoverflow topics but nothing helps me - I tried with but didn't help. The same with versions 2.0, 2.1, 2.2.
I have such dependencies in my pom.xml:
dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.3.6.Final</version>
</dependency>
It would be great if I could create an entityManager and then connect to database (on localhost) and perform some queries...
Thanks!

PersistenceException :
If you use the EntityManagerFactory in a JavaEE environment you need to define the transaction-type to RESOURCE_LOCAL in your persistence.xml :
<persistence-unit name="mypersistenceunit" transaction-type="RESOURCE_LOCAL">
For more informations about the EntityManager and difference beetween transaction-type JTA and RESOURCE_LOCAL in the persistence.xml see this answer.
Unrecognized JPA persistence.xml XSD version :
In your persistence.xml :
I dont see the persistence enclosing tag </persistence> after </persistence-unit> at the end.
You are using an old URL for the XSD Schema Location, change your <persistence ... > to :
<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_1.xsd"
version="2.1">
Informations from Oracle :
Starting with the 2.1 version, the Java Persistence API Schemas share the namespace, http://xmlns.jcp.org/xml/ns/persistence/. Previous versions used the namespace http://java.sun.com/xml/ns/persistence/.

Related

Kumuluzee JPA Persistence settings outside persistence unit

We are trying to configure the Kumuluz JPA.
We would like to tailor the Persistence Unit programmatically and for that, we need a handle on the PersistenceUnit Properties. This is already pre-packaged inside the kumuluz jpa dependency and we have apparently no way of getting a handle on the properties at runtime.
Has anyone had the same problem of having to set the properties at runtime? Can you please share your methods?
You cannot access persistence.xml configuration in runtime, nor would it make any sense to do so, since persistence.xml is read by JPA provider only at the very beginning of the application start-up.
You can, however, configure persistence.xml during build time with maven configuration, by using Maven Resources Plugin. For example:
pom.xml:
<project>
...
<properties>
<db.action>create</db.action>
</properties>
...
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
...
</project>
persistence.xml:
<?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_1.xsd"
version="2.1">
<persistence-unit name="kumuluzee-samples-jpa" transaction-type="JTA">
<jta-data-source>jdbc/CustomersDS</jta-data-source>
<class>com.kumuluz.ee.samples.jpa.Customer</class>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="${db.action}"/>
</properties>
</persistence-unit>
</persistence>

No Persistence provider for EntityManager named test

I'm trying to develop a Java EE web application using JPA to manage the DB.
When I try to retrieve the rows from my database I've an error :
"No Persistence provider for EntityManager named test"
The code of my function using JPA is :
EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
EntityManager em =emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
#SuppressWarnings("unchecked")
List<InputHStock> iph = em.createQuery("from Student").getResultList();
for (Iterator<InputHStock> iterator = iph.iterator(); iterator.hasNext();) {
InputHStock student = (InputHStock) iterator.next();
System.out.println(student.getLocationCode());
}
tx.commit();
} catch (Exception e) {
tx.rollback();
}
}
My persistence.xml file :
<?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="test">
<class>application.InputHStock</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:#localhost:1521:xe" />
<property name="javax.persistence.jdbc.user" value="testSQL" />
<property name="javax.persistence.jdbc.password" value="testpwd1" />
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
</properties>
</persistence-unit>
</persistence>
Here is my arborescence in eclipse :
I've also tried using the Eclipse tool to include JPA but without success.
Note that the connection to the database is working as I can retrieve my data when using directly JDBC.
Your persistence.xml is lacking a persistence provider. As you use EclipseLink, add
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
inside of the persistence-unit tag.
I think "hibernate-entitymanager" dependency missing in your pom.xml
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.3.6.Final</version>
</dependency>
Probably you are using hibernate-core in pom. Use hibernate-entitymanager instead of hibernate-core for JPA.
Also requires provider tag in persistence.xml.
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
I was dealing with this error in a somewhat misleading scenario. Hibernate dependency was correctly added, persistence.xml was fine.
Turns out I hadn't included the database driver dependency on Maven.
I thought it wouldn't be relevant to my test, since I was not going to hit a DB. Just wanted to test the Query API.

Persistence error in JPA

I am creating a small JPA project.
But I am receiving the below error, when I run the main class.
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named ClientAccount
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
at com.infinite.ClientAccountMain.main(ClientAccountMain.java:12)
the name in the persistence unit is the same used in the entity manager factory
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("ClientAccount");
<?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="ClientAccount">
<description>My Persistence Unit</description>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.infinite.Order</class>
<class>com.infinite.Account</class>
<class>com.infinite.Client</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://localhost:3306;databaseName=testdatabase" />
<property name="javax.persistence.jdbc.user" value="test"/>
<property name="javax.persistence.jdbc.password" value="test"/>
</properties>
</persistence-unit>
I think that Neil Stockton is right and if you search a bit you will find your answer. Although I think that must be something in relation with your provider information.
Try to put something like this.
For OpenJPA
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
Source information ---> http://openjpa.apache.org/builds/1.0.3/apache-openjpa-
1.0.3/docs/manual/jpa_overview_persistence.html
For Hibernate
<provider>org.hibernate.ejb.HibernatePersistence</provider>
There are multiple causes for this error:
You do not have the specified JPA provider on your classpath (check the JARs).
You did not define the persistence unit in your persistence.xml file.
Your persistence provider did not find your persistence.xml file (usually it is in META-INF/persistence.xml). Check that it is on the right place.

Setting Glassfish 4 with jdbc automatically rather to work around manually

I got following local setting:
Eclipse Kepler
Maven 3
Glassfish 4
I want to run JSF with MySql.JSF 2.0 works fine on the server.The problem I got is the connection to the database.
I did all settings at the admin of glassfish and here is the persistence.xml I got so far:
<?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="actors" transaction-type="JTA">
<jta-data-source>jdbc/example</jta-data-source>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
Maven dependency for MySQL:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
Get a jdbc not found exception...
The question is how can I setup Glassfish to use the maven dependency so that I don't need to integrate the jdbc.jar manually ?
I am afraid according to the official documentation you have to do it this way

what's wrong with my #PersistenceContext?

I'm using Eclipse Juno, Glassfish 3.1.2, and MySQL 5.1.
I'm building a simple EJB & JSF application. I created the following eclipse projects:
appEAR <-- the EAR file
appEJB <-- contains UserService.java EJB
appJPA <-- contains UserDAO.java EJB, and User.java object
appWeb <-- contains index.jsp
It's just a skeleton right now, but I can deploy the app and see the index.jsp
Next, I tried to add the following to the UserDAO ...
#PersistenceContext
EntityManager em;
But then when the app tries to republish, it gives me the error:
'Publishing to GlassFish 3.1.2 at localhost...' has encountered a problem. cannot Deploy appEar
There are no other details.
When I remove the two lines of #PersistenceContent code, the app deploys again.
Also, the persistence.xml file n the appJPA project is as follows:
<?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="appJPA">
<class>app.model.User</class>
</persistence-unit>
</persistence>
Please help ... what am I missing? I'm rather stuck.
Your persistence.xml is incomplete , you need to provide Connection properties to specify the provider ,which DB to connect etc
Heres an example using hibernate as the JPA provided
<persistence-unit name="educationPU"
transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.coe.jpa.StudentProfile</class>
<properties>
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/COE" />
<property name="hibernate.connection.username" value="root" />
<property name="show_sql" value="true" />
<property name="dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
and heres a more Generic one
I am very new to glassfish, JPA and so on and I have really problems with setting that up. What I am planning to do is a simple RESTful service with a persistent backend. I am using glassfish3 as application server and already deployed a simple REST service with the jersey-library. Now I want to provide access to a database via JPA. Glassfish is shipped with JavaDB/derby and EclipseLink, is that right? So, I want to use that :-)
I created a persistence.xml in META-INF:
<?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="myPU" transaction-type="JTA">
<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.ClientDataSource" /> <!-- org.apache.derby.jdbc.EmbeddedDriver -->
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/sample;create=true" />
<property name="javax.persistence.jdbc.user" value="APP" />
<property name="javax.persistence.jdbc.password" value="APP" />
<property name="eclipselink.ddl-generation" value="create-tables" />
</properties>
</persistence-unit>
</persistence>
I don't use glassfish. But I think the reason is that you didn't specify any datasource in the persistence.xml. you should do this in it, which you can use jndi or other way. and second, you should define the entityManagerFactory bean in spring context xml file.
Did you add the datasource in glasfish ? You will need to add the mysql jdbc drivers too. In Java EE, it's the persistence container (inside the server) which will create and manage the datasource for you.
See http://www.albeesonline.com/blog/2008/08/06/creating-and-configuring-a-mysql-datasource-in-glassfish-application-server/