use the one-to-many or many-to-one

左心房为你撑大大i 提交于 2019-12-06 04:37:05
Nilesh

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.

A Task cannot exist without a Department I suppose, but a Department can exist without a Task associated with it. So the dependency is on the Task to attach itself to the Department, hence should be the owning side in the relationship. Many-to-One should be your choice here.

Just by looking at the structure, I would choose many-to-one.
If a task has to be reassigned to another department, then only the task object needs to be altered, not the original department object.

From java collections, a Set does not allow duplicates, whereas a List might

You should define a join table called DEPARTMENT_TASKS and have a ManyToMany relationship between them.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!