我正在hibernate的帮助下开发一个电子购物程序。因为我在jsp,servlet,hibernate上是一个非常新鲜的初学者,我不知道如何在关系表中使用附加列进行多对多关系映射。我已经阅读了几个教程,但大多数都专注于注释映射和hibernate社区的文档,但它们都不适合我的情况。
我有三个表movie_information
,order_
和order_details
(关系),其中order_details
的结构如下所示。
order_details
-------------
order_id FK
movie_id FK
quantity
-------------
我正在尝试使用四个POJO类来实现多对多关系。这是代码。
MovieInformation.java
// other variable, constructor, getter, setter
private Set<OrderDetails> orderDetails = new HashSet<OrderDetails>();
Order.java
// other variable, constructor, getter, setter
private Set<OrderDetails> orderDetails = new HashSet<OrderDetails>();
OrderDetails.java
private OrderDetailsPK primaryKey;
private Order order;
private MovieInformation movie;
private int quantity;
OrderDetailsPK.java
private long orderId;
private long movieId;
MovieInformation.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>
Order.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>
OrderDetails.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"/>
当我尝试运行测试主方法时,从程序抛出异常
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
在我读完其他人的问题之后,我相信我的代码中的错误是由错误的映射代码引起的。希望你们能帮助我解决这个问题。如果提供了一些带有额外列的多对多映射的简单示例,那就太好了。
答案 0 :(得分:0)
我相信你的代码是干净的,因为控制台告诉你MappingNotFoundException
一旦加载了hibernate文件,它就找不到了。确保模型bean包中的hibernate文件不在源代码中。