Version 3.0
Mapping documents:
company.hbm.xml:
Code:
<hibernate-mapping package="at.rehdie.test.company">
<import class="at.rehdie.test.product.Product"/>
<class name="Company" table="company">
<composite-id name="id" class="CompanyId">
<key-property name="id" column="id" length="150"/>
</composite-id>
<property name="name" type="string" length="100"/>
<bag name="products" inverse="true">
<key column="company"/>
<one-to-many class="Product"/>
</bag>
</class>
</hibernate-mapping>
product.hbm.xml:
Code:
<hibernate-mapping package="at.rehdie.test.product">
<class name="Product" table="product">
<composite-id name="id" class="ProductId">
<key-property name="id" column="id" length="150"/>
</composite-id>
<property name="name" type="string" length="100"/>
<many-to-one name="company" class="Company"
not-null="false" lazy="true" />
</class>
</hibernate-mapping>
hibernate.cfg.xml:
Code:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
.... some more properties
<mapping resource="at/rehdie/test/company/company.hbm.xml"/>
<mapping resource="at/rehdie/test/product/product.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Full stack trace of any exception that occurs:org.hibernate.MappingException: Error reading resource: at/rehdie/test/product/product.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:448)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1312)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1284)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1266)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1233)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1161)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1147)
at at.rehdie.test.HibernateUtil.<clinit>(HibernateUtil.java:25)
at at.rehdie.test.Tests.newSession(Tests.java:296)
at at.rehdie.test.Tests.testCompanyProduct2(Tests.java:233)
at at.rehdie.test.Tests.main(Tests.java:309)
Caused by: org.hibernate.MappingException: duplicate import: Product
at org.hibernate.cfg.Mappings.addImport(Mappings.java:101)
at org.hibernate.cfg.HbmBinder.bindPersistentClassCommonValues(HbmBinder.java:542)
at org.hibernate.cfg.HbmBinder.bindClass(HbmBinder.java:487)
at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:233)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:151)
at org.hibernate.cfg.Configuration.add(Configuration.java:359)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:396)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:445)
... 10 more
I used the import-element to simplify the use of Product in the Mapping of Company. Is anything wrong with my mapping (I did not find anything in the FAQ). There are several posts for the same issue and all responses say "check, if you have your mapping twice in your config-file". Since this is not the case I'm quite confused.
There is no duplicate <mapping..> entry in the configuration.
I would suggest the following change in the class org.hibernate.cfg.Mappings (this should fix that problem):
Code:
public void addImport(String className, String rename) throws ....
{
if ( imports.put(rename, className)!=null )
throw new MappingException("duplicate import: " + rename);
}
could be changed to
Code:
public void addImport(String className, String rename) throws ..
{
String oldClassName = imports.put(rename, className);
if (oldClassName!=null && !oldClassName.equals(className))
throw new MappingException("duplicate import: " + rename);
}
Should I create a Jira Issue?