使用一对多或多对一的方式 [英] use the one-to-many or many-to-one

查看:128
本文介绍了使用一对多或多对一的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个班:

部门任务

一个部门可以有许多任务。一个任务只能属于一个部门。



那么使用一对多还是多对一?

 一对多
class Department {
private设置任务;
}
类任务{
......
}
//
Department.hbm.xml
....
< set name =tasks>
< key column =departId/>
< / set>
.....






多to-one

  class Department {

}
class Task {
部门离开;
}
//
Task.hbm.xml
....
< property name =depart>
< / property>
.....






什么是有什么区别?使用set和list的区别是什么?



和使用list的例子(xml configuration)?

解决方案

您的选择取决于两件事:


  1. 什么是使用这种关系的各种情况。对于例如如果您需要按部门列出您的任务,那么您将需要一对多。自从使用Hibernate以来,加载它们也很容易。


  2. 如果您按部门更新任务,则还需要其他关系。

  3. b
    $ b

    但是,请记住,这些决定深深植根于您的域模型以及您想要如何构建它。什么是使用这些实体的用例。我强烈建议阅读此 SO线程,此外观看Eric Evans的视频



    希望有所帮助。


    Two class:

    Department Task

    One department can have many tasks. One task can only belong to one department.

    So use one-to-many or many-to-one?


    one-to-many
    class Department{
      private Set tasks;
    }
    class Task{
      ......
    }
    //
    Department.hbm.xml
    ....
    <set name="tasks">
        <key column="departId" />
        <one-to-many class="Task" />
    </set>
    .....
    


    many-to-one

    class Department{
    
    }
    class Task{
      Department depart;
    }
    //
    Task.hbm.xml
    ....
    <property name="depart">
        <many-to-one class="Department" />
    </property>
    .....
    


    What's the difference?

    BTW,what is the difference between use the set and list?

    And example using list(xml configuration)?

    解决方案

    Your choices would depend on 2 things:

    1. What are various scenarios under which this relationships are used. For e.g. if you need to list your tasks by departments than One to Many is what you would need. It would also be easy for you to load them once since you are using Hibernate.

    2. If you update your tasks by department then you need the other relationship too.

    However, remember this decisions have deep roots into your domain model and how you want to structure it. What are the usecases under which these entities are used. I would strongly recommend reading this SO thread and moreover watch this video by Eric Evans.

    Hope that helps.

    这篇关于使用一对多或多对一的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆