提问者:小点点

使用HibernateXML映射额外列映射多对多关系的困难


我正在hibernate的帮助下开发一个电子购物程序。因为我是jsp、servlet、hibernate的新手,我不知道如何使用关系表中的附加列进行多对多关系映射。我已经阅读了几篇教程,但其中大部分都是关于hibernate社区的注释映射和文档,但没有一篇适合我的情况。

我有三个表,分别是电影信息表、订单详情表和订单详情表(关系表),其中订单详情表的结构如下所示。

order_details
-------------
order_id FK
movie_id FK
quantity
-------------

我正在尝试使用四个POJO类来实现多对多关系。代码来了。

电影信息。Java语言

// other variable, constructor, getter, setter
private Set<OrderDetails> orderDetails = new HashSet<OrderDetails>();

顺序Java语言

// other variable, constructor, getter, setter
private Set<OrderDetails> orderDetails = new HashSet<OrderDetails>();

订单Details.java

private OrderDetailsPK primaryKey;
private Order order;
private MovieInformation movie;
private int quantity;    

OrderDetailsPK。Java语言

private long orderId;
private long movieId;

电影信息。hbm。xml

<id column="movie_id" name="movieId" type="long">
    <generator class="native"/>
</id>

 // some property

<set name="orderDetails" table="order_details" inverse="true" lazy="true" fetch="select">
    <key>
        <column name="movie_id" not-null="true" />
    </key>
    <one-to-many class="cart.hibernate.orderDetails.orderDetails" />
</set>

顺序hbm。xml

<id column="order_id" name="orderId" type="long">
    <generator class="native"/>
</id>

//some properties

<set name="orderDetails" table="order_details" inverse="true" lazy="true" fetch="select">
    <key>
        <column name="order_id" not-null="true" />
    </key>
    <one-to-many class="cart.hibernate.orderDetails.orderDetails" />
</set>

订单Details.hbm.xml

<composite-id name="orderDetailsPK" class="cart.hibernate.orderDetailsPK.OrderDetailsPK">
  <key-property column="order_id" name="orderId" type="long"/>
  <key-property column="movie_id" name="movieId" type="long"/>
</composite-id>

<many-to-one name="order" class="cart.hibernate.order.Order" insert="false" update="false"/>
<many-to-one name="movie" class="cart.hibernate.movieInformation.MovieInformation" insert="false" update="false"/>
<property column="quantity" name="quantity" type="int"/>

OrderDetailsPK。hbm。xml

<class name="cart.hibernate.orderDetailsPK.OrderDetailsPK" >
  <property  name="orderId" type="long"/>
<property name="movieId" type="long"/>

hibernate.cfg.cml

<mapping class="cart.hibernate.MovieInformation" package="cart.hibernate.movieInformation" resource="cart/hibernate/movieInformation/MovieInformation.hbm.xml"/>        
<mapping class="cart.hibernate.Order" package="cart.hibernate.order" resource="cart/hibernate/order/Order.hbm.xml"/>
<mapping class="cart.hibernate.OrderDetails" package="cart.hibernate.orderDetails" resource="cart/hibernate/orderDetails/OrderDetails.hbm.xml"/>
<mapping class="cart.hibernate.OrderDetailsPK" package="cart.hibernate.orderDetailsPK" resource="cart.hibernate.orderDetailsPK/OrderDetailsPK.hbm.xml"/>

当我尝试运行测试main方法时,程序引发了异常

887 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : 
cart.hibernate.orderDetailsPK/OrderDetailsPK.hbm.xml
Initial SessionFactory creation failed.org.hibernate.MappingNotFoundException: resource: cart.hibernate.orderDetailsPK/OrderDetailsPK.hbm.xml not found
Exception in thread "main" java.lang.ExceptionInInitializerError
    at cart.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:28)
    at cart.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:18)
    at cart.hibernate.order.ManageOrder.main(ManageOrder.java:40)
Caused by: org.hibernate.MappingNotFoundException: resource: cart.hibernate.orderDetailsPK/OrderDetailsPK.hbm.xml not found
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:799)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2344)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2310)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2290)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2243)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
    at cart.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:23)
    ... 2 more

在我阅读了其他人提出的问题后,我相信我的代码中的错误是由错误的映射代码引起的。希望你们能帮助我解决这个问题。如果提供一些带有额外列的多对多映射的简单示例就好了。


共1个答案

匿名用户

我相信你的代码是干净的,因为控制台告诉你MappingNotFoundException一旦加载了hibernate文件,就找不到了。确保模型bean包中的hibernate文件不在源代码中。