Persistence object cannot find persistence unit from persistence.xml
Asked Answered
B

4

3

Environment: Windows 7, NetBean 6.9 (the one including GlassFish v3, Java EE 6), MySQL server

I've created tables in MySQL database and using NetBean's capability by right click on the project and choose "create entity from database" (Sorry if the wording is wrong because my NetBean is in Japanese)

This will create Entities.

Now I went to test if I can access DB through Entity Manager.

Tmp.java

package local.test.tmp;
import Resources.Users;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import java.util.List;
/**
 *
 * @author m-t
 */
public class Tmp {

    private static EntityManagerFactory factory;
    private static final String WEB_PU = "WebApplication1PU";

    public static void main(String args[]) {
        factory = Persistence.createEntityManagerFactory(WEB_PU);
        EntityManager em = factory.createEntityManager();
        //read existing entries and write to concole
        Query q = em.createQuery("select * from users");

        List<Users> userList = q.getResultList();
        for(Users u : userList) {
            System.out.println(u);
        } 
    }
}

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="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <!--   orignal
  <persistence-unit name="WebApplication1PU" transaction-type="JTA">
    <jta-data-source>masatoJNDI</jta-data-source>
    <properties/>
  </persistence-unit>
  -->

  <persistence-unit name="WebApplication1PU" transaction-type="JTA">
    <jta-data-source>masatoJNDI</jta-data-source>
    <properties>
        <property name="toplink.jdbc.user" value="masato" />
        <property name="toplink.jdbc.password" value="foobar" />
    </properties>
  </persistence-unit>
</persistence>

When I run Tmp.java, I can see it fails at:

factory = Persistence.createEntityManagerFactory(WEB_PU);

Error msg:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence 
provider for EntityManager named WebApplication1PU
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:84)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at local.test.tmp.Tmp.main(Tmp.java:24)

I've checked the spelling of persistence unit "WebApplication1PU" in test code matches with with persistence.xml and it is correct.

persistence.xml is located ... it's in Japanese I don't know how in English :(

Let me try..

project
 |-------web page
 |-------test package
 |-------lib
 |-------enterprise bean
 |-------*** file  //blah file, I can't translate..
               |-----MNIFEST.MF
               |-----faces-cofig.xml
               |-----persistence.xml  //HERE!!

Since the failure is at the beginning where Factory tries to locate persistence unit in persistence.xml, I am totally confused. Is there anything I should be verifying?

please let me know if you need more clarification.

UPDATE

I've tried suggested possible solutions but no luck.. Same error message is returned. Yes, netbean 6.9 default provider is toplink.

What I did:

Added provider to 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="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

  <persistence-unit name="NoJSFPU" transaction-type="JTA">
    <provider>oracle.toplink.essentials.PersistenceProvider</provider>
    <jta-data-source>masatoJNDI</jta-data-source>

    <properties>
        <property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/mytest"/>
        <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        <property name="toplink.jdbc.user" value="masato" />
        <property name="toplink.jdbc.password" value="mocha123" />
    </properties>
  </persistence-unit>
</persistence>

Seems like most of people are being success with my situation. I am now start thinking there may be problem with how I run the test file or where the file is located?

Tmp.java that I executes from netbean is located at:

project
|------web page
|------source package
|          |-----------local.test.session
           |-----------local.test.tmp
                           |------------------Tmp.java //HERE

I'm not sure if it matters though.

UPDATE 2

I've added toplink lib containing toplink-essential.jar and toplink-essential-agent.jar to the lib directory of my project and now I'm getting new error on top of original one but I believe I'm getting closer.

Looks like version attribute in persistence.xml has an issue...???

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named WebApplication1PU: Provider named oracle.toplink.essentials.PersistenceProvider threw unexpected exception at create EntityManagerFactory: 
oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException
Local Exception Stack: 
Exception [TOPLINK-30005] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: sun.misc.Launcher$AppClassLoader@f6a746
Internal Exception: Exception [TOPLINK-30004] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while processing persistence.xml from URL: file:/C:/Users/m-takayashiki/Documents/NetBeansProjects/NoJSF/build/web/WEB-INF/classes/
Internal Exception: 
(1. cvc-complex-type.3.1: Value '2.0' of attribute 'version' of element 'persistence' is not valid with respect to the corresponding attribute use. Attribute 'version' has a fixed value of '1.0'.)
        at oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:143)
        at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.createEntityManagerFactory(EntityManagerFactoryProvider.java:169)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:110)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
        at local.test.tmp.Tmp.main(Tmp.java:24)

UPDATE 3

After some research, find out version number needs to be 1.0 compliant so did I applied the fix and got new error.

 <?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="NoJSFPU" transaction-type="JTA">
    <provider>oracle.toplink.essentials.PersistenceProvider</provider>
    <jta-data-source>masatoJNDI</jta-data-source>
    <class>local.test.session.Addresses</class>
    <class>local.test.session.Users</class>
    <properties>
        <property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/mytest"/>
        <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        <property name="toplink.jdbc.user" value="masato" />
        <property name="toplink.jdbc.password" value="mocha123" />
    </properties>
  </persistence-unit>
</persistence>

It's weird because I already specified class in persistence.xml above :(

Exception in thread "main" javax.persistence.PersistenceException: Exception [TOPLINK-7060]
 (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.ValidationException
Exception Description: Cannot acquire data source [masatoJNDI].
Internal Exception: javax.naming.NoInitialContextException: Need to specify class name in
 environment or system property, or as an applet parameter, or in an application resource 
file:  java.naming.factory.initial

UPDATE 4

I don't think this is called "solved" but after long try, I decided to just run my test code in the web app instead of running individually from netbean. I had to create web.xml myself since GlassFish does not provide it. (I see sun-web.xml but it wasn't the substitution) Then created servlet, define mapping in the web.xml and added my test code snippet to the servlet and tried on the browser. It worked, successfully obtained data from database.

I wonder what is the reason why it wasn't working when I try running just the Tmp.java (my test file) from Netbean...

Blades answered 14/12, 2010 at 1:28 Comment(2)
where are your files located? the persistence.xml should be in classpath's meta-infSpirant
possible duplicate: #1158659Sendal
S
3

I think you may need to specify your persistence provider. See here.

For example:

<provider>org.hibernate.ejb.HibernatePersistence</provider>

or

<provider>oracle.toplink.essentials.PersistenceProvider</provider>

Your "original" persistence unit relies on the server to wire in details like the provider, but when you run it outside of the server context you need to specify a few things - one of them being the provider.

See here.

Sendal answered 14/12, 2010 at 1:46 Comment(4)
@masato-san you will probably also need to add the jar files for the persistence manager to your classpath. Glassfish comes with toplink if I remember correctly. This is only because you are running it outside the appserver. If you through together a app to do the same ting as the class you gave and deployed it, I bet it would work.Mellophone
@Jschoen: do you know what the name of the jar would be for persistence manager? I've looked GlasshFish server 3 directory inside of lib directory of my project and it has javax.persistence.jar but i don't think this is what you talking about.Blades
@masato-san I think it is just toplink.jar, but you might need others. There is a list of them here download.oracle.com/docs/cd/B32110_01/web.1013/b28217/…. You could also just download them from there.Mellophone
@Jschoen: Hmm I added toplink.jar to lib directory of my project but no effect.. maybe something that I think correct is actually being wrong.Blades
L
5

Please specify the <provider> for your <persistence-unit>:

<!-- EclipseLink -->
<provider>
  org.eclipse.persistence.jpa.PersistenceProvider
</provider>

<!-- Hibernate -->
<provider>
  org.hibernate.ejb.HibernatePersistence
</provider>

<!-- Toplink -->
<provider>
 oracle.toplink.essentials.PersistenceProvider
</provider>

And make your persistence-unit name is correct and file path is also in correct context path.

Lincoln answered 14/12, 2010 at 2:11 Comment(0)
S
3

I think you may need to specify your persistence provider. See here.

For example:

<provider>org.hibernate.ejb.HibernatePersistence</provider>

or

<provider>oracle.toplink.essentials.PersistenceProvider</provider>

Your "original" persistence unit relies on the server to wire in details like the provider, but when you run it outside of the server context you need to specify a few things - one of them being the provider.

See here.

Sendal answered 14/12, 2010 at 1:46 Comment(4)
@masato-san you will probably also need to add the jar files for the persistence manager to your classpath. Glassfish comes with toplink if I remember correctly. This is only because you are running it outside the appserver. If you through together a app to do the same ting as the class you gave and deployed it, I bet it would work.Mellophone
@Jschoen: do you know what the name of the jar would be for persistence manager? I've looked GlasshFish server 3 directory inside of lib directory of my project and it has javax.persistence.jar but i don't think this is what you talking about.Blades
@masato-san I think it is just toplink.jar, but you might need others. There is a list of them here download.oracle.com/docs/cd/B32110_01/web.1013/b28217/…. You could also just download them from there.Mellophone
@Jschoen: Hmm I added toplink.jar to lib directory of my project but no effect.. maybe something that I think correct is actually being wrong.Blades
K
0

I imagine the problem is this line:

<jta-data-source>masatoJNDI</jta-data-source>

which, unless you're running in a JNDI-enabled context will cause the type of error you initially saw. Try creating a persistence.xml file in your test/resources folder and removing that line in the test version of the xml file.

Kirby answered 4/3, 2015 at 23:29 Comment(0)
M
0

I think you may need to add a jndi context,you should create a jndi.properties file in your jar, and add the below content in it.

java.naming.factory.initial=bitronix.tm.jndi.BitronixInitialContextFactory
Machicolate answered 25/11, 2016 at 4:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.