如何修复org.hibernate.MappingException取消映射类

时间:2019-07-13 15:19:01

标签: java mysql hibernate one-to-many hibernate-mapping

编辑:

问题解决了,我没有在XML文件中写实体的确切位置,那里只有一个标签。

我正在尝试将这些类映射到数据库,但出现此错误:

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)

我试图找到我的问题出在哪里,我读到了这个异常,但我不明白程序中的问题出在哪里。

课程:

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;

}

表格:

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

XML文件:

<?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>

我认为问题是我声明的表,也许与表不匹配 xml文件。

1 个答案:

答案 0 :(得分:0)

您需要将* hbm.xml配置的顺序更改为:

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

将“实体”包添加到您的所有一对多映射中,如下所示:

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