我正在尝试使用OpenJPA进行正向映射的过程。我想为此目的使用MappingTool,但我继续获取MetaDataException。我提供persistence.xmlorm.xml文件作为输入,并在类路径上提供了已编译的Java类,这些类定义了orm.xml文件中的对象。

我这样运行MappingTool:

java -cp  /home/vic/openmdx/dev/openmdx2/jre-1.6/core/lib/openmdx-base.jar:/home/vic/Downloads/apache-openjpa-2.2.1/src/java:/home/vic/Downloads/apache-openjpa-2.2.1/openjpa-2.2.1.jar:/home/vic/Downloads/apache-openjpa-2.2.1/openjpa-all-2.2.1.jar org.apache.openjpa.jdbc.meta.MappingTool -p persistence.xml -schemaAction build orm.xml


编译的类文件在/home/vic/Downloads/apache-openjpa-2.2.1/src/java中的位置。

输出是这样的:


 32  openjpa  INFO   [main] openjpa.jdbc.JDBC - Using dictionary class org.apache.openjpa.jdbc.sql.MySQLDictionary".
106  openjpa  INFO   [main] openjpa.Tool - Mapping tool running on type "class org.test.classdiag.jpa3.Animal" with action "buildSchema".
407  openjpa  WARN   [main] openjpa.Runtime - Found no persistent property in "org.omg.model1.jpa3.Attribute$Slice"
412  openjpa  WARN   [main] openjpa.Runtime - Found no persistent property in "org.omg.model1.jpa3.StructureField$Slice"
441  openjpa  WARN   [main] openjpa.Runtime - Found no persistent property in "org.omg.model1.jpa3.Reference$Slice"
455  openjpa  WARN   [main] openjpa.Runtime - Found no persistent property in "org.omg.model1.jpa3.Parameter$Slice"
465  openjpa  WARN   [main] openjpa.Runtime - Found no persistent property in "org.omg.model1.jpa3.Constant$Slice"
504  openjpa  WARN   [main] openjpa.Runtime - Found no persistent property in "org.omg.model1.jpa3.Import$Slice"
515  openjpa  WARN   [main] openjpa.Runtime - Found no persistent property in "org.omg.model1.jpa3.Constraint$Slice"
Exception in thread "main" <openjpa-2.2.1-r422266:1396819 fatal user error> org.apache.openjpa.util.MetaDataException: Type "class org.test.classdiag.jpa3.Animal" loaded by sun.misc.Launcher$AppClassLoader@54f690e4 does not have persistence metadata.
  at org.apache.openjpa.jdbc.meta.MappingTool.getMapping(MappingTool.java:688)
at org.apache.openjpa.jdbc.meta.MappingTool.buildSchema(MappingTool.java:754)
at org.apache.openjpa.jdbc.meta.MappingTool.run(MappingTool.java:652)
at org.apache.openjpa.jdbc.meta.MappingTool.run(MappingTool.java:1087)
at org.apache.openjpa.jdbc.meta.MappingTool.run(MappingTool.java:1001)
at org.apache.openjpa.jdbc.meta.MappingTool$1.run(MappingTool.java:937)
at org.apache.openjpa.lib.conf.Configurations.launchRunnable(Configurations.java:748)
at org.apache.openjpa.lib.conf.Configurations.runAgainstAllAnchors(Configurations.java:738)
at org.apache.openjpa.jdbc.meta.MappingTool.main(MappingTool.java:932)



我的persistence.xml:

<?xml version="1.0"?>
<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="openjpa"  transaction-type="RESOURCE_LOCAL">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
         <class>org.test.classdiag.jpa3.Animal</class>
                 <class>org.test.classdiag.jpa3.Cat</class>
                 <class>org.test.classdiag.jpa3.Dog</class>

        <properties>
            <property name="openjpa.Log" value="SQL=TRACE"/>
            <!-- <property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" /> -->
            <property name="openjpa.jdbc.MappingDefaults" value="IndexLogicalForeignKeys=false,IndexDiscriminator=false"/>
            <property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/TEST_OPENMDX"/>
            <property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
            <property name="openjpa.ConnectionUserName" value="root"/>
            <property name="openjpa.ConnectionPassword" value="root"/>
        </properties>
    </persistence-unit>
</persistence>


我对orm.xmlAnimal

<access>FIELD</access>
<entity class="org.test.classdiag.jpa3.Animal">
  <table name="OTT0CLAG_ANIMAL"/>
  <inheritance strategy="JOINED"/>
  <discriminator-value>org:test:classdiag:Animal</discriminator-value>
  <discriminator-column name="DTYPE" discriminator-type="STRING"/>
  <attributes>
    <basic name="colour">
      <column name="COLOUR" nullable="true"/>
    </basic>
    <basic name="breed">
      <column name="BREED" nullable="true"/>
    </basic>
    <basic name="name">
      <column name="NAME" nullable="true"/>
    </basic>
    <one-to-many name="openmdxjdoSlices" mapped-by="openmdxjdoIdentity">
      <map-key name="openmdxjdoIndex"/>
    </one-to-many>
  </attributes>
</entity>

最佳答案

这是因为MappingTool使用JDBCConfigurationImpl作为默认值。配置MappingTool以将数据源用于分布式配置(DistributedJDBCConfigurationImpl)。

为了做到这一点,您需要从main复制MappingTool代码并为conf创建一个DistributedJDBCConfigurationImpl,如下所示:

String[] arguments = new String[] { "-action", "buildSchema", "-foreignKeys", "false", "-openjpa.Tool", "false" };
Options opts = new Options();
final String[] args = opts.setFromCmdLine(arguments);
Configurations.runAgainstAllAnchors(opts, new Configurations.Runnable() {

    @Override
    public boolean run(Options opts) throws IOException, SQLException {
        JDBCConfiguration conf = new DistributedJDBCConfigurationImpl();
        conf.setLogFactory(new NoneLogFactory());
        try {
            return MappingTool.run(conf, args, opts);
        } finally {
            conf.close();
        }
    }
});


并将参数更改为您自己的参数。运行自己的工具实现后,一切都会正常运行。

09-05 21:31