Dark Star1
Dark Star1

Reputation: 7403

Hibernate association references unmapped class exception

I have the following class:

public class Car implements Comparable<Car>{

private long idCar;

private String model;

private String immat; //Car License Plate

private Company company;

private Manufacturer manufacturer;    

private Calendar registrationDate;

private Calendar lastControlDate;

//Has empty constructor + Getters and setters here onwards...

and it's hibernate configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="Car" table="cars" lazy="true"> 
    <id name="idCar" type="long" column="idCar">
      <generator class="native" />
    </id>

    <property name="model" type="string" column="model" />
    <property name="immat" type="string" column="immat" />
    <property name="registrationDate" type="date" column="registrationDate" />
    <property name="lastControlDate" type="date" column="lastControlDate" />

    <many-to-one name="company" class="fr.model.company.Company" column="idCompany"
     not-null="true" />
    <many-to-one name="manufacturer" class="fr.model.component.Manufacturer"
             column="idManufacturer" not-null="true" />    

  </class>
</hibernate-mapping>

and the Manufacturer class:

public class Manufacturer implements Comparable<Manufacturer> {

private Long idManufacturer;
private String name;

I keep getting the association unmapped references error but I can't figure out why so far.

Edit: Manufacturer mapping -

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="Manufacturer" table="manufacturer">
      <id name="idManufacturer" type="long" column="idManufacturer">
            <generator class="native" />
    </id>

    <property name="name" type="string" not-null="true" />

  </class>

</hibernate-mapping>

Exception:

Initial SessionFactory creation failed.org.hibernate.MappingException: 
Association references unmapped class: fr.synapture.model.company.Car

Upvotes: 8

Views: 42761

Answers (6)

Raj
Raj

Reputation: 1

There may be an simple file name error for the xml configs you can cross check those

Upvotes: 0

Jorge Santos Neill
Jorge Santos Neill

Reputation: 1785

To resolve it issue, you should add the xml files to hibernate configuration , please following the next steps:

  1. Open hibernate perspective
  2. Select your configuration, for example "hibernate configuration"
  3. Right click in your configuration
  4. Select edit configuration
  5. Open Mappings tab
  6. Click on Add button
  7. Find and select "Manufacturer.hbm.xml" and "Car.hbm.xml"
  8. Click on Ok button
  9. Click on Ok button

With this steps your will be resolve

Upvotes: 0

Parth Kansara
Parth Kansara

Reputation: 1

Just include both the xmls Manufacturer.hbm.xml and Car.hbm.xml in the

<mapping resource="Manufacturer.hbm.xml"/>
<mapping resource="Car.hbm.xml"/>

in your hibernate.cfg.xml

Upvotes: 0

zchpit
zchpit

Reputation: 3121

In my app I have nHibernate mapping project separate with logic project (2 projects). I have set all mappings in "model project" and... have this error: Association references unmapped class:

In my sytuation, I had to set properties of MyClass.hbm.xml to "Build Action - Enabled Resource".

Upvotes: 4

Alex Barnes
Alex Barnes

Reputation: 7218

Initial SessionFactory creation failed.org.hibernate.MappingException: Association references unmapped class: fr.synapture.model.company.Car

This suggests that you've mapped a class which the session factory doesn't know about. You need to include Car in the Session Factory configuration.

To confirm this please could you include the Hibernate configuration in your questions.

Upvotes: 15

ChssPly76
ChssPly76

Reputation: 100706

Manufacturer class should be mapped as well, and so should Company since both are referenced from Car mapping.

Upvotes: 1

Related Questions