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...
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.