【问题标题】:Add mapping resource from other JAR从其他 JAR 添加映射资源
【发布时间】:2013-02-27 09:20:09
【问题描述】:

我有一个 jar 文件,其中包含一些我想添加的映射文件,然后作为映射资源添加到 hibernate.cfg.xml 中,因为我在 org.hibernate.cfg.Configuration 中添加了 JAR..

    Configuration cfg=new Configuration();
    cfg.addJar(new File("C:\\Users\\amoghs\\.m2\\repository\\mkcl\\os\\personServiceBL\\1.0.1\\personServiceBL-1.0.1.jar"));
    cfg.configure("hibernate.cfg.xml");
    SchemaExport se=new SchemaExport(cfg);
    se.setDelimiter("\n#-----------------------------------------------------------------------------------");
    se.setOutputFile("E:\\ADFCreateScript.sql");
    se.create(true,true);

我在 hibernate.cfg.xml 中添加了..

<mapping resource="org/mkcl/personservices/models/Person.hbm.xml" />

Person.hbm.xml 这个 xml 文件在 jar 文件中。

当我执行 java 应用程序时,我得到了这个异常......

 Exception in thread "main" org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3415)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueue(Configuration.java:3404)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3392)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1341)
    at org.hibernate.cfg.Configuration.generateDropSchemaScript(Configuration.java:931)
    at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:188)
    at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:156)
    at mkcl.accreditation.model.GenrateSchema.main(GenrateSchema.java:34)
Caused by: org.hibernate.PropertyNotFoundException: field [countryCode] not found on java.util.List
    at org.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:182)
    at org.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:174)
    at org.hibernate.property.DirectPropertyAccessor.getGetter(DirectPropertyAccessor.java:197)
    at org.hibernate.internal.util.ReflectHelper.getter(ReflectHelper.java:253)
    at org.hibernate.internal.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:229)
    at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:326)
    at org.hibernate.cfg.HbmBinder.createProperty(HbmBinder.java:2286)
    at org.hibernate.cfg.HbmBinder.bindComponent(HbmBinder.java:1994)
    at org.hibernate.cfg.HbmBinder.createClassProperties(HbmBinder.java:2191)
    at org.hibernate.cfg.HbmBinder.createClassProperties(HbmBinder.java:2141)
    at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:407)
    at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:322)
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:173)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3412)

谁能帮帮我?

【问题讨论】:

    标签: hibernate hibernate-mapping


    【解决方案1】:

    如果将整个 jar 添加到 Hibernate Configuration,那么它包含的所有映射 (*.hbm.xml) 都会自动添加到 Configuration。无需在 hibernate.cfg.xml 文件中添加以下行:

    <mapping resource="org/mkcl/personservices/models/Person.hbm.xml"/>
    

    【讨论】:

      猜你喜欢
      • 2014-06-21
      • 2010-11-09
      • 1970-01-01
      • 2013-05-14
      • 2011-07-20
      • 2012-07-16
      • 1970-01-01
      • 2012-12-09
      • 2018-11-18
      相关资源
      最近更新 更多