OpenJPA 2.1.1 - Cannot find the declaration of element 'persistence' - persistence

I just downloaded http://www.apache.org/dyn/closer.cgi/openejb/4.0.0-beta-1/apache-tomee-1.0.0-beta-1-webprofile.zip to use OpenEJB with OpenJPA2.1.1.
I can't get my persistence.xml working.
The top of the stack trace:
org.xml.sax.SAXException:
file:/D:/Workspaces/sandbox/tomcat_ejb_jpa2_tomEE/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/testEE/WEB-INF/classes/META-INF/persistence.xml
[Location: Line: 2, C: 248]: org.xml.sax.SAXParseException: cvc-elt.1:
Cannot find the declaration of element 'persistence'.
My 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="testEE">
</persistence-unit>
</persistence>
I've read, that the problem should be solved with the namespace declaration (xmlns="http://java.sun.com/xml/ns/persistence"), but it still doesn't work.

<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="2.0">
<persistence-unit name="jpa-lib">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<class>com.dav.jpa.entity.Employee</class>
<class>com.dav.jpa.entity.Department</class>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:oracle:thin:#localhost:1521:DVD" />
<property name="openjpa.ConnectionDriverName" value="oracle.jdbc.OracleDriver" />
<property name="openjpa.ConnectionUserName" value="scott" />
<property name="openjpa.ConnectionPassword" value="tiger" />
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
</properties>
</persistence-unit>
</persistence>
This is working for me..

Does the problem disappear if you simplify your tag a bit?
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
...
</persistence>
I just encountered the same problem and fixed it using above BUT preceding this I just had
<persistence version="2.0">
as indicated in the OpenJpa manual.

Related

Eclipse: How to remove Red Indicator from Persistance.xml when there is no problem when opened in editor?

Following is my persistance.xml which is not showing any error:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2"
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_2.xsd">
<persistence-unit name="IntegratorMasterdataDS"
transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/datasources/IasDS</jta-data-source>
<properties>
<property name="hibernate.archive.autodetection"
value="class" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
Following is red indicator shown by eclipse:

Cannot create EntityManager from valid persistence.xml

I'm trying to use JPA from Play 2.0 Scala application. In my code I have:
val factory = Persistence.createEntityManagerFactory("devcrowd")
I also have persistence.xml in package META-INF:
<?xml version="1.0" encoding="UTF-8"?>
<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_0.xsd" version="2.0">
<persistence-unit name="devcrowd" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.username" value="xxxx"/>
<property name="hibernate.connection.password" value="xxxx"/>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/devcrowd"/>
</properties>
</persistence-unit>
</persistence>
This persistence.xml seems valid to me, but the factory cannot be created:
PersistenceException: No Persistence provider for EntityManager named devcrowd
You are missing the Hibernate library/ies at deploy time.

persistence.xml with Glassfish 3.1.1

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>
Then I created a field in my resource, where I want to access/store som data:
#PersistenceUnit(unitName = "myPU")
EntityManagerFactory emf;
But "emf" is always NULL :-(
I guess that my persistence.xml is not configured appropriate.
Would be really glad if someone has a hint, what I am doing wrong...
thanks!
I think it is better to create JNDI for db connection . You can do it easly with GlassFish.
Firstly create connection pool (you will set db connection settings);
Resources->JDBC->JDBC Connection Pools
After that crate JNDI name for this pool ;
Resources->JDBC->JDBC Resources
So lets say you set JNDI name as "dbCon"
And here your 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="myPU" transaction-type="JTA">
<jta-data-source>dbCon</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
Note : You must copy your jdbc jar to \glassfish-3.1.1\glassfish\domains\domain1\lib\ext
I have the solution now for my problem.
Here is the corresponding configuration:
glassfish 3.1.1
built-in JavaDB/derby database: jdbc/__default
glassfish's JPA, which is eclipselink
(JAX RS: Jersey, which is shipped with glassfish)
So, you have to create the folder "META-INF" wihtin your src folder and put the persistence.xml there:
<?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>
<jta-data-source>jdbc/__default</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
</properties>
</persistence-unit>
</persistence>
I created the .xml previously in the META-INF of WebContent, and that is wrong.
You also do not have to reference any additional libraries, since you have the glassfish modules added.
Now I have created a JavaBean, where I do inject the PersistenceUnit:
#Stateless
public class StorageService {
#PersistenceContext(unitName = "myPU")
EntityManager em;
...
}
And this one is injected in my Resource-Classes of the Jersey-Servlets:
#Path("/someres")
#Produces(MediaType.APPLICATION_XML)
#Stateless
public class SomeRes {
#EJB
StorageService storageService;
...
}
The injections do only work if the classes are marked as "#Stateless".
I have not tried with RESTful service, but I guess that should not matter. I noticed you are using persistence.xml for version 1. Any specific reason?
Following persistence.xml works for me:
<?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="myPU">
<properties>
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
</properties>
</persistence-unit>
</persistence>
Hope this helps.

How use Hibernate Tools to execute JPQL Queries?

I need to make some tests with JPQL, so I'm trying do that with Hibernate Tools, but when I try open the session factory appears this : Could not locate TransactionManager as showed below:
Here is my 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="SuaParte">
<jta-data-source>jdbc/suaparte_ds</jta-data-source>
<class>entity.Area</class>
//classes..
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
<property name="hibernate.connection.datasource" value="jdbc/suaparte_ds"/>
</properties>
</persistence-unit>
</persistence>
I had the same problem and after a long search for solution I found one publication.
I tried the advice given in the link Hibernate tools: Could not find datasource and it worked.

Database settings in persistence.xml not used

I'm new to JPA, and to try to teach myself, I'm setting up a tiny web application and deploying to Glassfish 3.1.
JPA works fine when I refer to a JNDI DataSource in persistence.xml, such as this:
<?xml version="1.0" encoding="UTF-8"?>
<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_1_0.xsd"
version="1.0">
<persistence-unit name="foo" transaction-type="JTA">
<jta-data-source>jdbc/foo</jta-data-source>
<class>my.app.Foo</class>
</persistence-unit>
</persistence>
But as far as I understand, it is supposed to be possible to put all my database connection settings into properties in persistence.xml. This may not be good practice, but it seems like it could be handy when I'm just experimenting, and perhaps during unit testing.
However, when I follow the examples I have found for this, persistence.xml seems to be just ignored and instead the default container-managed DataSource, jndi/__default is used. This is a Derby instance that is not running.
I've tried this file for an ephemeral in-memory Derby instance:
<?xml version="1.0" encoding="UTF-8"?>
<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_0.xsd"
version="2.0">
<persistence-unit name="foo" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>my.app.Foo</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:NxtMv;create=true"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>
I have also tried this for a PostgreSQL server (which works when accessed through JNDI):
<?xml version="1.0" encoding="UTF-8"?>
<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_0.xsd"
version="2.0">
<persistence-unit name="foo" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>my.app.Foo</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/foo"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.user" value="myuser"/>
<property name="javax.persistence.jdbc.password" value="secret"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>
There's probably some irrelevant cruft in those files which has accumulated during my countless tries and retries.
What am I missing here?
You cannot use manually configured datasource with transaction-type="JTA".
JPA Spec says:
A transaction-type of JTA
assumes that a JTA data source will be provided—either as specified by the jta-data-source element
or provided by the container.
Try to use transaction-type="RESOURCE_LOCAL" instead (though I'm not sure how would it work with container-managed transactions, if you use them).