简体   繁体   English

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

[英]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. 问题解决了,我没有在XML文件中写实体的确切位置,其中有多个标签。

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文件:

<?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. 我认为问题是我声明的表,也许与xml文件不匹配。

You need to change the order of *hbm.xml configs to: 您需要将* hbm.xml配置的顺序更改为:

<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: 并将“ entities”包添加到您所有的一对多映射中,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM