Exception javax.persistence.PersistenceException: No Persistence provider for EntityManager - jpa

We've been working on this for days and we are stumped. This is supposed to be an easy tutortial using TopLink. We are trying to get this to work before we do our real web app. This is the following exception we get:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named pu1:
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 client.Client.main(Client.java:45)
Java Result: 1
this happens after executing this line from our emf driver class:
emf = Persistence.createEntityManagerFactory("pu1");
I'm assuming the problem is in our persistence.xml file (which is in the correct folder (WEB-INF/classes/META-INF). Also netbeans generated the xml file for us which is:
<?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="JPAExamplePU" transaction-type="JTA">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<jta-data-source>SomeDB</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="toplink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
We were also thinking it may be an adding a library issue or something along that line. Any help is much appreciated. Thanks

You have the wrong persistence unit name. Use the one from the xml (i.e. the one defined with <persistence-unit name="..."):
emf = Persistence.createEntityManagerFactory("JPAExamplePU");

Related

How do I get my project to use a container managed Enitymanager lifecycle rather an Application managed one?

I've been at this for a while - hoping to get some help. The first persistence.xml gives the output below it. The alternate persistence.xml crashes with: javax.persistence.PersistenceException: No Persistence provider for EntityManager named com.topcat_mavenproject1_jar_1.0-SNAPSHOTPU Please let me know if there's anything I can ad to make this clearer.
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
#Stateless
public class NewClass {
#PersistenceContext(unitName ="com.topcat_mavenproject1_jar_1.0-SNAPSHOTPU")
static EntityManager containerManagedEntityManager;
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory(
"com.topcat_mavenproject1_jar_1.0-SNAPSHOTPU");
EntityManager applicationManagedEntityManager = emf.createEntityManager();
System.out.println("Container managed entityManager: "+containerManagedEntityManager);
System.out.println( "Application managed entityManager: " +applicationManagedEntityManager);
}
}
The output:
[EL Info]: 2016-08-16 01:51:13.395--ServerSession(33510911)--EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd
[EL Info]: connection: 2016-08-16 01:51:13.535--ServerSession(33510911)--file:/Users/me/NetBeansProjects/mavenproject1/target/classes/_com.topcat_mavenproject1_jar_1.0-SNAPSHOTPU login successful
[EL Warning]: metamodel: 2016-08-16 01:51:13.552--The collection of metamodel types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units. Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element
Container managed entityManager: null
Application managed entityManager: org.eclipse.persistence.internal.jpa.EntityManagerImpl#6f139fc9
My 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="com.topcat_mavenproject1_jar_1.0-SNAPSHOTPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/nutrition_DB"/>
<property name="javax.persistence.jdbc.user" value="app"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.password" value="pass"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
Alternate persistence.xml:
<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="persistenceUnit" transaction-type="JTA">
<jta-data-source>jdbc/dataSource</jta-data-source>
<class>test.domain.TestEntity</class>
</persistence-unit>
</persistence>
In order to have a container-managed entity manager, your should use the Java Transaction API (JTA) instead of RESOURCE_LOCAL. The different between them are :
JTA means the persistence is managed by the application server
RESOURCE_LOCAL means the persistence is managed by yourself.
You can see more difference for Persistence unit as RESOURCE_LOCAL or JTA?
So in your case, I suggest you to do the following :
Make sure you're in a Java EE environment, e.g. Tomcat, WildFly and not a Java SE.
Modify your PU com.topcat_mavenproject1_jar_1.0-SNAPSHOTPU, set transaction-type="JTA"
Make sure that your JTA data source (DS) is configured in the server and activated
Describe this data source in your persistence XML in tag jta-data-source
Inject the EntityManager into you java class using PersistenceContext as you've done. Since the persistence is managed by the application server, do not use entity manager factory anymore except you know exactly what you're doing.
However, it seems that you're under the Java SE environment. In the case, there isn't any Java EE container (application server). And therefore, you cannot benefit the JTA configuration :( You must use RESOURCE_LOCAL mode and manage everything yourself.

tomee - how to use RESOURCE_LOCAL datasource

I have some classes (ejb, webservices, mdb, etc..) that can use JTA. For some classes I need RESOURCE_LOCAL (can't be injected). However I can't get tomee to reference the jndi name of RESOURCE_LOCAL. How do you setup tomee and RESOURCE_LOCAL? I can't seem to find one good example online, I would prefer not to put any usernames and passwords in my persistence.xml file.
tomee.xml has this:
<Resource id="MYDS" type="DataSource">
JdbcDriver com.mysql.jdbc.Driver
JdbcUrl jdbc:mysql://127.0.0.1:3306/maestro
UserName myusername
Password mypassword
JtaManaged false
</Resource>
persistence.xml looks like:
<?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 = "MYDS" transaction-type = "RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>MYDS</non-jta-data-source>
</persistence-unit>
</persistence>
I am using name MYDS in EntityManagerFactory lookup, but get this error:
Caused by: org.hibernate.engine.jndi.JndiException: Unable to lookup JNDI name [MYDS]
at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:117)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:115)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:852)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:845)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:75)
... 36 more
Caused by: javax.naming.NameNotFoundException: Name [MYDS] is not bound in this Context. Unable to find [MYDS].
at org.apache.naming.NamingContext.lookup(NamingContext.java:817)
at org.apache.naming.NamingContext.lookup(NamingContext.java:160)
at org.apache.naming.NamingContext.lookup(NamingContext.java:828)
at org.apache.naming.NamingContext.lookup(NamingContext.java:160)
the solution seems to be this (still verifying):
(not very intuative or documented, adding openejb:Resource to JPA and JPA doesn't work, removing it from RESOURCE_LOCAL and RESOURCE_LOCAL doesn't work)
<?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 = "MYDS" transaction-type = "RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>openejb:Resource/MYDS</non-jta-data-source>
</persistence-unit>
<persistence-unit name="MYDSJPA" transaction-type = "JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>MYDS</jta-data-source>
</properties>
</persistence-unit>
</persistence>
how do you get your persistence unit? Manually?
If using injection:
#PersistenceUnit EntityManagerFactory emf;
#PersistenceContect EntityManager em;
TomEE resolves the datasource for you from its short name (id in tomee.xml) otherwise you need to give it the full JNDI name which I think is java:openejb/Resource/MYDS

Getting hibernate persistence-unit classes from both ORM and OGM

I'm trying to use both of hibernate's orm and ogm in the same application.
In my persistence.xml I have two persistence-unit:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="orm-mysql">
...
<class>...</class>
...
<class>...</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
...
</persistence-unit>
<persistence-unit name="ogm-mongodb" transaction-type="JTA">
...
<class>...</class>
...
<class>...</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
...
</persistence-unit>
...
</persistence>
I want to use the hibernate orm native api with both orm and ogm, and so I need to add the annotated classes to two different MetadataSources.
Since I have the persistence units I thought that I can do this:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("orm-mysql");
Set<ManagedType<?>> managedTypes = entityManagerFactory.getMetamodel().getManagedTypes();
for (ManagedType type : managedTypes) {
ormSources.addAnnotatedClass(type.getJavaType());
}
entityManagerFactory = Persistence.createEntityManagerFactory("ogm-mongo");
managedTypes = entityManagerFactory.getMetamodel().getManagedTypes();
for (ManagedType type : managedTypes) {
ogmSources.addAnnotatedClass(type.getJavaType());
}
The problem is that in the first iteration, of the orm classes, I get both the orm and ogm classes that are included in orm-mysql and ogm-mongo.
The second time around it's fine though, I get only the ogm-mongo classes.
Can't figure out what's wrong, any ideas?
In case anyone makes this stupid mistake as well, I had:
<persistence-unit name="orm-mysql">
...
<class>...</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.archive.autodetection" value="class"/>
...
</properties>
</persistence-unit>
Removing the hibernate.archive.autodetection property solved the problem.

EclipseLink does not work on Netbeans, is this normal?

It'll be weeks that I'm stuck with EclipseLink. I can not persist an object in my database. I use netbeans 7.3. I encountered this problem when I started designing a web application. What follows is the approach I have adopted. It may be that I do without me realize a mistake.
After that netbeans has finished generating the project files I configured the jndi. Then I converted automatically with netbeans, the database tables in entity object.
here is the link
then, from these classes, I created their JPAController . (Always automatically with netbeans)
and finally, as a test, I just instantiate the description of "Outils" and leave the fields empty id. Since the latter automatically increment in the database, if the persistence is done well, I should have an id when I appear with out the console.
<body>
<h1>Hello World!</h1>
<%
Outils o = new Outils();
o.setDesignation("hammers");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Test_EclipseLinkPU");
UserTransaction utx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
OutilsJpaController o_ctrl = new OutilsJpaController(utx, emf);
o_ctrl.create(o);
out.println("this is the id of hammer " + o.toString());
%>
</body>
and I get as result: Outils[ id=null ].
I have no error or on glassfish even less about the debugger.
Ps : Here are the persistence.xml file
<?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="Test_EclipseLinkPU" transaction-type="JTA">
<jta-data-source>test_data_source</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
Thank you for your future is in your answers and listening for any additional information.
(Not really an answer but I couldn't show this in a comment)
To increase logging your persistence.xml should look like this:
<?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="Test_EclipseLinkPU" transaction-type="JTA">
<jta-data-source>test_data_source</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
</properties>
</persistence-unit>
</persistence>
You need to commit or flush the transaction before anything will be written to the database. If you are using IDENTITY sequencing (I strongly recommend not using IDENTITY, use TABLE or SEQUENCE if db supports it), then the Id cannot be allocated until the insert occurs, so a persist() will not assign the Id (as it does for TABLE and SEQUENCE).
What does your create() method do? How is your Id mapped?

Is it possible to set persistence unit default catalog at runtime

For the moment I use the table annotation containing the catalog
#Table(catalog = "Mycatalog", schema = "MySchema", name = "MyTable")
But the catalog name should be made configurable.
The persistence.xml file can not be changed per deployment, and the datasource default database should be set to TempDB. (Another process is locking the catalog quite often and the driver keeps a connection open to the datasource default database) so I am limited to change the default catalog using properties passed to the EntityManagerFactory.
EntityManagerFactory emf = provider.createEntityManagerFactory(
"default", properties);
Is it possible to set the persistence unit default catalog in the properties?
I am using eclipselink as JPA provider.
I was able to use different Catalogs as long as they are in the XML configuration files.
First I removed all #Table annotations from my entity classes and created two orm files.
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 persistence_1_0.xsd"
version="1.0">
<persistence-unit name="storeone" transaction-type="RESOURCE_LOCAL">
<mapping-file>META-INF/orm-storeone.xml</mapping-file>
</persistence-unit>
<persistence-unit name="storetwo" transaction-type="RESOURCE_LOCAL">
<mapping-file>META-INF/orm-storetwo.xml</mapping-file>
</persistence-unit>
</persistence>
orm-storeone-xml:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings 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"
version="1.0">
<entity class="com.package.EntityClass">
<table catalog="SomeCatalog" schema="SomeSchema" name="SomeTable" />
</entity>
</entity-mappings>
And then in the code where I create my EntityManager I am able to choose between the two catalogs.
String persistencUnitName = "storeone";
EntityManagerFactory emf = provider.createEntityManagerFactory(
persistencUnitName , map);
this way I can change between catalogs at runtime, (but I still cannot add new catalogs at runtime).