帮助识别数据库实体之间的关系(用于休眠映射)
我有一个网络应用程序,其中使用 hibernate 并使用 ant 部署在 tomcat 上。我在源目录中创建了一个包含所有映射值的 hibernate.cfg.xml 。
我的应用程序有 ShoppingCart 、CartItem 和 ItemForSale 类。我需要映射由数据库表表示的 CartItem 和 ItemForSale 类之间的关系。我尝试按如下所示进行计算。我不确定这是否是正确的方法.. 我想了解您对此事的建议/意见。请帮忙。CartItem
有一个 ItemForSale 字段和数量。
class CartItem{
ItemForSale item;
int quantity;
...
}
class ItemForSale{
String name;
double price;
}
假设我有这两个类的一些实例,
saleitem1 = new ItemForSale("pizza",20.0);
saleitem2 = new ItemForSale("pastry",10.0);
saleitem3 = new ItemForSale("cake",30.0);
cartitem1 = new CartItem(saleitem1,1);
cartitem2 = new CartItem(saleitem1,2);
cartitem3 = new CartItem(saleitem2,1);
一个 caritem 一次不能包含多个 saleitem,
我在下图中显示了映射..绿线显示有效关系,而红线显示这两个集合之间的无效关系
由此,我推断,存在多对一关系购物车项目和之间ItemForSale.So,在我的 CartItem.hbm.xml 文件中,我必须写
<class name="shop.cart.CartItem" table="CARTITEM">
<id name="cartItem_id" column="CARTITEM_ID" type="long">
<generator class="native"/>
</id>
<property name="quantity" type="int" column="QUANTITY" />
<many-to-one name="saleitem" class="shop.domain.ItemForSale" column="ITEM_FOR_SALE_ID" lazy="false" />
</class>
这是正确的表示吗?还是我的处理方式有问题?
谢谢马克
I have a web app in which I use hibernate and deploy on tomcat using ant .I have created a hibernate.cfg.xml in source directory with all mapping values .
My app has a ShoppingCart ,CartItem and ItemForSale classes.I need to map the relation between CartItem and ItemForSale classes which are represented by database tables.I tried to work it out as shown below..I am not sure if this is the right way..
I would like your advice /opinion in this matter..Please help..
A CartItem has an ItemForSale field and quantity .
class CartItem{
ItemForSale item;
int quantity;
...
}
class ItemForSale{
String name;
double price;
}
Suppose I have some instances of both these classes,
saleitem1 = new ItemForSale("pizza",20.0);
saleitem2 = new ItemForSale("pastry",10.0);
saleitem3 = new ItemForSale("cake",30.0);
cartitem1 = new CartItem(saleitem1,1);
cartitem2 = new CartItem(saleitem1,2);
cartitem3 = new CartItem(saleitem2,1);
A cartitem cannot contain more than one saleitem at a time,
I have shown the mappings in a diagram below..the green lines show valid relations while red line shows an invalid relation between these two sets
From this,I deduce that ,there is a many-to-one relation between CartItem and ItemForSale.So,in my CartItem.hbm.xml file,I have to write
<class name="shop.cart.CartItem" table="CARTITEM">
<id name="cartItem_id" column="CARTITEM_ID" type="long">
<generator class="native"/>
</id>
<property name="quantity" type="int" column="QUANTITY" />
<many-to-one name="saleitem" class="shop.domain.ItemForSale" column="ITEM_FOR_SALE_ID" lazy="false" />
</class>
Is this the correct representation? Or is there something wrong with the way I worked it out?
thanks
mark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来正确...您已经完成了单向映射...
您还可以进行双向映射(变化不大,但它使您可以访问与购物车相关的所有项目。
looks correct... you have done a unidirectional mapping...
you could also do a bidirectional mapping (doesn't change much but it gives you access to all items related to a cart.