简体   繁体   English

帮助识别数据库实体之间的关系(用于 hibernate 映射)

[英]help to identify relation between database entites (for hibernate mapping)

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. 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..我的应用程序有一个 ShoppingCart、CartItem 和 ItemForSale 类。我需要 map 由数据库表表示的 CartItem 和 ItemForSale 类之间的关系。我试图按如下所示进行计算..我不确定这是否是正确的方法..我想在这件事上征求你的意见/意见..请帮忙..

A CartItem has an ItemForSale field and quantity. CartItem 具有 ItemForSale 字段和数量。

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,一个 caritem 一次不能包含多个 saleitem,

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由此,我推断,CartItem 和 ItemForSale 之间存在多对一的关系。所以,在我的 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>

Is this the correct representation?这是正确的表示吗? Or is there something wrong with the way I worked it out?还是我的解决方法有问题?

thanks谢谢

mark标记

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.您还可以进行双向映射(变化不大,但它使您可以访问与购物车相关的所有项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM