【发布时间】:2013-12-01 14:19:45
【问题描述】:
我正在借助 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;
电影信息.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
在阅读了其他人提出的问题后,我认为我的代码中的错误是由错误的映射代码引起的。希望你们能帮我解决这个问题。如果提供一些带有额外列的多对多映射的简单示例,那就太好了。
【问题讨论】:
-
你对
OrderDetailsPK使用内部类吗? -
不,这是一个单独的课程。我应该使用内部类吗?
-
只是一个评论:越来越难找到帮助你处理 Hibernate XML 文件的人,因为这些是你多年前配置 Hibernate 的方式,当时 Java 还处于第 4 版,注解还没有存在。世界已经在发展,我们现在几乎是在 Java 8 上,标准的方法是使用注解。你最好帮自己一个忙,学会使用它们。您不仅会以标准方式做事,而且会以更简单的方式做事。
-
好的,谢谢您的提醒,我将在下一个项目中使用注释。这是我第一次使用hibernate和jsp,所以我对hibernate的概念了解不多。这就是为什么我在开始我的项目时只是随机选择使用 xml。
标签: java xml hibernate nhibernate-mapping