Search code examples
javamysqlhibernateone-to-manyhibernate-mapping

How to fix org.hibernate.MappingException unmapping class


EDIT :

The problem solved,I didnt write the exactly loaction of the entites in the XML files,Where the one-many-tag.

Im trying to map these classes to database and i get this error :

Exception in thread "main" org.hibernate.MappingException: Association references unmapped class: Reservation
    at org.hibernate.cfg.HbmBinder.bindCollectionSecondPass(HbmBinder.java:2557)
    at org.hibernate.cfg.HbmBinder$CollectionSecondPass.secondPass(HbmBinder.java:2808)
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70)
    at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1695)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
    at entities.chairandtabletest.main(chairandtabletest.java:39)

I tried to find where is my problem,I read about this exception and i dont understand where is the problem in my program.

The classes :

public class Customer {

private Integer id;
    private String fullName;
    private String address;
    private String email;
    private String phoneNumber;
    private List<Reservation> reservations = new ArrayList<Reservation>();
    }
public class Reservation{

private Integer id;
    private String date;
    private Integer totalSum;
    private boolean isDeliever;

    private List<Item> items = new ArrayList<Item>();

}
public class Item {

private Integer id;
    private String name;
    private Integer price;

}

The tables :

CREATE TABLE ITEM(
    item_id INT NOT NULL AUTO_INCREMENT,
    item_name VARCHAR(40),
    item_price INT NOT NULL,
    reservation_id INT,
    PRIMARY KEY(item_id)
    );
    CREATE TABLE RESERVATION(
    reservation_id INT NOT NULL AUTO_INCREMENT,
    reservarion_date VARCHAR(255),
    reservtion_delivery BOOL,
    reservation_total_sum INT,
    customer_id INT,
    PRIMARY KEY(reservation_id)
    );
     CREATE TABLE CUSTOMERS(
    customer_id INT NOT NULL AUTO_INCREMENT,
    customer_full_name VARCHAR(40),
    customer_address VARCHAR(255),
    customer_email VARCHAR(255),
    customer_phone_number VARCHAR(20),
    PRIMARY KEY(customer_id)
    );

The XML files :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/chairandtable?serverTimezone=UTC</property>
    <property name="hibernate.connection.username">aliali</property>
    <property name="hibernate.connection.password">password</property>
    <mapping resource="props/customer.hbm.xml"/>
    <mapping resource="props/reservation.hbm.xml"/>
    <mapping resource="props/item.hbm.xml"/>
  </session-factory>
</hibernate-configuration>
<?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="entities.Customer" table="customers">

    <meta attribute="class-description">
        This class contain customers details.
    </meta>
    <id name="id" type="integer" column="customer_id">
        <generator class="identity"/>
    </id>
    <bag name="reservations" cascade="all">

        <key column= "customer_id"/>
        <one-to-many class="Reservation"/>

    </bag>

    <property name="fullName" column="customer_full_name" type="string"/>
    <property name="address" column="customer_adress" type="string"/>
    <property name="email" column="customer_email" type="string"/>
    <property name="phoneNumber" column="customer_phone_number" type="string"/>

     </class>

</hibernate-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 ="entities.Reservation" table="reservation">

    <meta attribute="class-description">
        This class contain reservation detail.
    </meta>
    <id name ="id" column="reservation_id" type="integer">
        <generator class="identity"/>
    </id>

    <bag name="items" cascade="all">

        <key column="reservation_id"/>
        <one-to-many class="Item"/>

    </bag>

    <property name="date" column="reservation_date" type="string"/>
    <property name="totalSum" column="reservation_total_sum" type="integer"/>
    <property name="isDeliver" column="reservtion_delivery" type="boolean"/>

</class>

</hibernate-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="entities.Item" table="item">

    <id name= "id" column="item_id" type="integer">

        <generator class="identity"/>

    </id>
    <property name="name" column="item_name" type="string"/>
    <property name="price" column="item_price" type="integer"/>

</class>
</hibernate-mapping>

I think the problem is the tables i declared , Maybe the are not matching the xml file.


Solution

  • You need to change the order of *hbm.xml configs to:

    <mapping resource="props/item.hbm.xml"/>
    <mapping resource="props/reservation.hbm.xml"/>
    <mapping resource="props/customer.hbm.xml"/>
    

    And add the "entities" package to all of your one-to-many mappings like this:

    <one-to-many class="entities.Item"/>