Java – the persistence object cannot be removed from persistence Persistence unit found in XML
Environment: Windows 7, netbean 6.9 (including GlassFish V3, Java EE 6), MySQL server
I have created a table in the MySQL database and use the netbean function by right clicking the item and selecting "create entity from database" (sorry, if the wording is wrong, because my netbean is Japanese)
This creates a solid
Now I'll test whether I can access the database through the 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. In 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 that it failed:
factory = Persistence.createEntityManagerFactory(WEB_PU);
Error message:
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 persistence unit "web application 1pu" and persistence XML matches the spelling, it is correct
persistence. XML is located in... It is Japanese. I don't know how to use 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!!
Because the failure is in the factory, try in persistence I was completely confused at the beginning of finding the persistence unit in XML Is there anything I should verify?
If you need more explanation, please let me know
UPDATE
I have tried to suggest possible solutions, but I have no luck Return the same error message Yes, the default provider for netbean 6.9 is TopLink
What did I do?
Added persistence XML provider
<?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>
It seems that most people are successful in my situation I'm beginning to think that there may be a problem with the running test file or the location of the file?
I executed TMP. From netbean Java is located at:
project |------web page |------source package | |-----------local.test.session |-----------local.test.tmp |------------------Tmp.java //HERE
I'm not sure if it's important
Update 2
I have included TopLink essential Jar and TopLink essential agent Add TopLink lib of jar to the Lib directory of my project. Now I receive new errors based on the original version, but I believe I'm getting closer and closer
Looks like persistence There is a problem with the version attribute in XML???
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, I found that the version number needs to be 1.0 compatible, so I applied the fix and got new errors
<?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 strange because I've been above persistence Class specified in XML:(
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 "resolved", but after a long attempt, I decided to run my test code only in the web application, not from netbean alone I have to create my own web XML, because GlassFish does not provide it (I see sun-web.xml, but it's not a replacement) then I create a servlet on the web The mapping is defined in XML, and my test code fragment is added to the servlet and tried on the browser It works and successfully obtains data from the database
I want to know when I try to run TMP from netbean What is the reason why Java (my test file) doesn't work
Solution
I think you might need to specify a persistence provider See here
For example:
<provider>org.hibernate.ejb.HibernatePersistence</provider>
or
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
Your "raw" persistence unit depends on the server to connect to the provider and other details, but when you run it outside the server context, you need to specify something - one of which is the provider
See here