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

标签 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)

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

类(class):

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

最佳答案

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

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

并将“entities”包添加到所有一对多映射中,如下所示:

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

关于java - 如何修复 org.hibernate.MappingException 取消映射类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57020412/

相关文章:

java - 如何避免空检查

java - 在 Java 应用程序中暂停游戏

sql-server - 如何使用 Spring Data JPA 在没有结果集的情况下执行 native SQL 服务器查询

java - Hibernate @ManyToOne getId() 导致获取

java - 一个 JMS 主题中可以排队多少条消息?

java - JAXWS-RI 模块需要每个服务接口(interface)的实现

java - android中不同屏幕尺寸的可绘制尺寸

mysql - 如何显示员工详细信息以及所有员工的总和(sal)?

mysql - 如何将一列的值复制到同一行中的另一列?

mysql - mysql 中的 GROUP BY 查询返回多个重复行