找不到元素“持久性”的声明(Eclipselink和HibernateOGM在一起)

时间:2018-12-16 15:46:01

标签: jpa java-ee eclipselink hibernate-ogm

几天来我一直在努力解决这个问题。 这段代码是一个大项目的一部分,显然我不能全部发布。 当实例化MyObj对象并尝试使其在Mysql(第一个持久性单元)和mongodb(第二个持久性单元)中持久化时,出现此错误。 在此错误附近进行搜索似乎导致某个地方的JPA不兼容。

persistence.xml(带有2个PU)

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
  version="2.1">
    <persistence-unit name="MYSQLPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
        <class>com.jpa.MyObj</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>

        <shared-cache-mode>NONE</shared-cache-mode>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mysqldev" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="eclipselink.logging.level" value="FINE" />
        </properties>
    </persistence-unit>
     <persistence-unit name="MONGOPU" transaction-type="JTA">
        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
        <class>com.jpa.MyObj</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.transaction.jta.platform"
                value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform" />
            <property name="com.arjuna.ats.jta.jtaTMImplementation"
                value="com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple" />
            <property name="com.arjuna.ats.jta.jtaUTImplementation"
                value="com.arjuna.ats.internal.jta.transaction.arjunacore.UserTransactionImple" />
            <property name="hibernate.ogm.datastore.create_database"
                value="true" />
            <property name="hibernate.ogm.datastore.provider"
                value="mongodb" />
            <property name="hibernate.ogm.datastore.database"
                value="ActingMongo" />
            <property name="hibernate.ogm.mongodb.host"
                value="127.0.0.1" />
            <property name="hibernate.ogm.mongodb.port" value="27017" />
        </properties>
    </persistence-unit>
</persistence>

主班

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.transaction.SystemException;

public class CreateMyObj {

    private static final Logger log = Logger.getAnonymousLogger();

    public static void main(String[] args) {

        //some proprietary objects
        PlatConfiguration configuration = new PlatConfiguration();
        Plat.init(configuration, DataManager.getInstance());

        MyObj me = new MyObj();
        me.setTitle("MyObj  title");
        me.setType("MyObj");
        me.setUserId(0);


        /*--------------------mysql persistence managed using some external proprietary libs ----------------------------*/
        try {
            DataManager.getInstance().save(me); //this object instanciates the EntityManagerFactory and the EntityManager and make the object persistent

            System.out.println("persistence successful");
        } catch (DataManagerException e) {
            System.out.println("persistence unsuccessful!");
            e.printStackTrace();
        }


        /*----------------------mongodb persistence -------------------*/
        //the following line throw the exception
EntityManagerFactory emfactory2 = 
Persistence.createEntityManagerFactory("MONGOPU");
        EntityManager entitymanager2 = emfactory2.createEntityManager();

         javax.transaction.TransactionManager tx = com.arjuna.ats.jta.TransactionManager.transactionManager();
            try {
                tx.begin();
                entitymanager2.joinTransaction();
                entitymanager2.persist(me);
                tx.commit();
                log.log(Level.INFO, "no sql Persist successful...");
            } catch (Exception re) {
                try {
                    tx.rollback();
                } catch (IllegalStateException | SecurityException | SystemException e) {
                    e.printStackTrace();
                }
                log.log(Level.SEVERE, "no sql Persist failed...", re);
                //throw re;
            } finally {
                if (entitymanager2 != null) {
                    entitymanager2.clear();
                    entitymanager2.close();
                }
            }

    }

}

这是完整的堆栈跟踪,显示错误来自Hibernate,而不是eclipselink

Exception in thread "main" javax.persistence.PersistenceException: Invalid persistence.xml.
Error parsing XML [line : -1, column : -1] : cvc-elt.1: impossibile trovare la dichiarazione dell'elemento "persistence".

    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.ogm.jpa.HibernateOgmPersistence.createEntityManagerFactory(HibernateOgmPersistence.java:46)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at com.helloproject.jpa.CreateMyObj.main(CreateMyObj.java:57)

1 个答案:

答案 0 :(得分:0)

您可以为persistence.xml尝试以下标头吗?

<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_1.xsd" version="2.1">