2

我正在尝试使用 OpenJPA 完成前向映射的过程。我想MappingTool为此目的使用 ,但我不断获得MetaDataException. 我提供文件persistence.xml作为orm.xml输入,并在定义orm.xml文件对象的类路径上提供已编译的 Java 类。

我像这样运行 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>
4

1 回答 1

0

这是因为它MappingToolJDBCConfigurationImpl用作默认值。配置MappingTool为使用数据源进行分布式配置 (DistributedJDBCConfigurationImpl)。

为了实现这一点,您需要从中复制main代码MappingTool并创建一个DistributedJDBCConfigurationImplfor conf,如下所示:

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();
        }
    }
});

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

于 2013-08-07T12:12:09.343 回答