I have a project where we use Hibernate-jpa-2.1 and get an EntityManager istance both in this way
#PersistenceContext(unitName = "common-api")
EntityManager em;
and this
em = Persistence.createEntityManagerFactory("common-api").createEntityManager();
because some classes aren't EJB and others are (I guess). The problem is that the createEntityManagerFactory fails with the error:
Caused by: javax.persistence.PersistenceException: Invalid persistence.xml.
Error parsing XML [line : -1, column : -1] : cvc-complex-type.2.4.a: Invalid content was found starting with element 'class'. One of '{"http://xmlns.jcp.org/xml/ns/persistence":validation-mode, "http://xmlns.jcp.org/xml/ns/persistence":properties}' is expected.
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.validate(PersistenceXmlParser.java:357)
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.loadUrl(PersistenceXmlParser.java:290)
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.parsePersistenceXml(PersistenceXmlParser.java:94)
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.doResolve(PersistenceXmlParser.java:84)
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.locatePersistenceUnits(PersistenceXmlParser.java:66)
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:80)
... 80 more
This is the 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="common-api" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>(...)</jta-data-source>
<class>(the only entity we have in this Maven module)</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<validation-mode>NONE</validation-mode>
<properties>
<property name="javax.persistence.validation.mode" value="NONE" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL57InnoDBDialect" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.order_updates" value="true" />
<property name="hibernate.max_fetch_depth" value="2" />
<property name="hibernate.use_sql_comments" value="true" />
<property name="hibernate.use_identifer_rollback" value="true" />
<property name="hibernate.jdbc.batch_size" value="0" />
<property name="hibernate.jdbc.batch_versioned_data" value="false" />
<property name="hibernate.jdbc.use_get_generated_keys" value="true" />
<property name="hibernate.hbm2ddl.auto" value="none"/>
</properties>
</persistence-unit>
</persistence>
I don't understand why this happens because my classes are in the same package, same maven module, use the same Hibernate entities and the same persistence.xml.
N.B.: Making this class an EJB isn't an option.
This is an error you should only get if the element order is wrong (and not the same as you posted here). The order of elements in persistence.xml is important during validation. It's described in the xsd. In each persistence-unit it is as follows:
description
provider
jta-data-source
non-jta-data-source
mapping-file
jar-file
class
exclude-unlisted-classes
shared-cache-mode
validation-mode
properties
Related
When i tried to deploy an application in weblogic, he give-me the follow exception:
Substituted for missing class Exception [EclipseLink-7298] (Eclipse
Persistence Services - 2.6.5.v20170607-b3d05bd) -
org.eclipse.persistence.exceptions.ValidationException Exception
Description: The mapping [associated] from the embedded ID class
[class br.com.webproj.web.model.associated.pk.AssmaintainPK] is an
invalid mapping for this class. An embeddable class that is used with
an embedded ID specification (attribute [AssmaintainPK] from the
source [class br.com.webproj.web.model.associated.AssMaintain]) can
only contain basic mappings. Either remove the non basic mapping or
change the embedded ID specification on the source to be embedded.
In my persistence.xml i have:
<?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="Web_PU">
<jta-data-source>JDBC/WEB</jta-data-source>
<jar-file>web-Model.jar</jar-file>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.hbm2ddl.auto" value="none" />
<property name="hibernate.show_sql" value="false" />
<!-- Cache Enabled -->
<property name="hibernate.cache.use_second_level_cache"
value="true" />
<property name="hibernate.cache.use_query_cache"
value="true" />
<property name="hibernate.cache.region.factory_class"
value="org.hibernate.cache.infinispan.JndiInfinispanRegionFactory" />
<property name="hibernate.cache.infinispan.cachemanager"
value="java:jboss/infinispan/container/hibernate" />
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.JBossTransactionManagerLookup" />
<property name="hibernate.cache.infinispan.statistics"
value="true" />
<!-- I TRIED INCLUDING THIS, BUT NOT SOLVE -->
<property name="eclipselink.exclude-eclipselink-orm"
value="false" />
<property name="eclipselink.orm.throw.exceptions"
value="false" />
<property name="eclipselink.orm.validate.schema"
value="false" />
</properties>
</persistence-unit>
</persistence>
I think this error occurs because the weblogic use the eclipse link, and the project an other version of jpa. But the error is in a jar included in project and i cant change and see the code. If the problem is this, what i can do to dont use the eclipse link of weblogic (obs: in JBoss the project runs normally).
I have application (.war) which uses entities from other jar file from different classpath. My persistance.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="EAP" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/pscDataSource2</jta-data-source>
<class>com.myPackage.entity.MyEntity</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.jdbc.batch_size" value="50" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.generate_statistics" value="false" />
<property name="hibernate.id.new_generator_mappings" value="false" />
<property name="hibernate.ejb.interceptor" value="com.myPackage.interceptor.DelegatingInterceptor" />
</properties>
</persistence-unit>
</persistence>
With this xml I load MyEntity class successfully. However I would like to avoid listing all necessary classes in persistance.xml file. What I've found is <property name="hibernate.archive.autodetection" value="class, hbm" /> But with such approach I need to list jar files from where I want to load entities. So my persistance.xml file now looks like below and works fine
<?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="EAP" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/pscDataSource2</jta-data-source>
<jar-file>lib/myJar.jar</jar-file> //new line
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.jdbc.batch_size" value="50" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.generate_statistics" value="false" />
<property name="hibernate.id.new_generator_mappings" value="false" />
<property name="hibernate.ejb.interceptor" value="com.myPackage.interceptor.DelegatingInterceptor" />
<property name="hibernate.archive.autodetection" value="class, hbm" /> //new line
</properties>
</persistence-unit>
</persistence>
Question: Is the any way to load entities from jars without listing them at all if I have dependencies on them from my application? I don't use Spring
i use this tag inside my persistence.xml and it worked for me. may be you should try it cause am using eclipselink implementation of JPA
<exclude-unlisted-classes>false</exclude-unlisted-classes>
According to Red Hat support empty <jar-file></jar-file> property is the solution. It works for me. So now my 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="EAP" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/pscDataSource2</jta-data-source>
<jar-file></jar-file> //fix
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.jdbc.batch_size" value="50" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.generate_statistics" value="false" />
<property name="hibernate.id.new_generator_mappings" value="false" />
<property name="hibernate.ejb.interceptor" value="com.myPackage.interceptor.DelegatingInterceptor" />
</properties>
</persistence-unit>
</persistence>
I am novice in JPA 2.0.
Can someone tell me the steps how to create a new session and then execute query in JPA ?
Please do needful.
I hope you have created a persistence.xml with a persistence unit tag. If you haven't then you need one here is the example
<?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="JPASamplePU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.cksoft.jpasample.entity.Person</class>
<class>org.cksoft.jpasample.entity.Profile</class>
<class>org.cksoft.jpasample.entity.Department</class>
<class>org.cksoft.jpasample.entity.Skills</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/MyApp_DB" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<!-- <property name="hibernate.cache.use_second_level_cache" value="true"
/> -->
<!-- <property name="hibernate.cache.use_query_cache" value="true" /> -->
</properties>
</persistence-unit>
</persistence>
After that you need to create EntityManagerFactory (if you are accessing it from main method)
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PERSISTENCE_UNIT_NAME");
EntityManager em = emf.createEntityManager();
Here is how we can get a transaction and begin it and commit it.
em.getTransaction().begin();
em.getTransaction().commit();
After this you can keep using the EntityManager or close it
em.close();
Session is hibernate specific class which is almost as equal as EntityManager.
Correct me guys if I am wrong or something is missing thank you.
I have created the OSGI bundle project with JPA 2.0 support. I am using OpenJpa as a JPA provider. when I run the bundle, I Could not create OpenJPAEntityManagerFactory. Please fine the code which I used to create OpenJPAEntityManagerFactory.
OpenJPAEntityManagerFactory emf = OpenJPAPersistence.createEntityManagerFactory(
"StudentServiceProvider",
"META-INF/persistence.xml");
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="StudentServiceProvider">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>com.student.serviceprovider.model.Student</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/NYL" />
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver" />
<property name="openjpa.ConnectionUserName" value="root" />
<property name="openjpa.ConnectionPassword" value="root" />
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
<property name="openjpa.RuntimeUnenhancedClasses" value="supported" />
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
<!-- To avoid foreign key issues -->
<property name="openjpa.jdbc.SchemaFactory" value="native(ForeignKeys=true)" />
<!-- Auto create tables -->
<property name="jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
</properties>
</persistence-unit>
</persistence>
In src folder I have META-INF and i created the persistence.xml with in that(META-INF/persistennce.xml).
Please let me know to resolve this issue.
It'd probably be helpful if you listed what the error is you are getting, but it might be because you have this line twice:
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
This is just a stab in the dark since you don't mention what the error is and I just happen to be getting an error message of "Equivalent property keys "openjpa.Log" and "Log" are specified in configuration." for something I'm working on.
I've been searching around and haven't been able to find a working fix. My persistence.xml file is located in /src/META-INF/persistence.xml, which from looking around, this is the correct location for it.
I'm using glassfish as the server, and I keep getting the following:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named pers
Here is my persistence file:
<?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="pers" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>cs.ee.assignment2.Client</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.archive.autodetection" value="class, hbm" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/assignment2" />
<property name="hibernate.connection.password" value="root" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
Any ideas on what the problem could be would be greatly appreciated.
Hmm.. make sure you are using PeristenceUnit not context in your entity class: see http://openejb.apache.org/jpa-concepts.html
Switch to "transaction" just to see if it fails too.
Also sometimes the old classes are not unloaded from the server properly if you are doing redeployment, you may have to shut it down and restart after redeploy.