【问题标题】:How to fix org.hibernate.MappingException unmapping class如何修复 org.hibernate.MappingException 取消映射类
【发布时间】:2019-11-23 00:21:49
【问题描述】:

编辑:

问题解决了,我没有在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 文件。

【问题讨论】:

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


    【解决方案1】:

    您需要将 *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"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-30
      • 2012-08-13
      • 2012-02-17
      • 2014-06-20
      • 2022-01-15
      • 2012-11-25
      相关资源
      最近更新 更多