JPA实体层次结构中的双向关系

huangapple go评论80阅读模式
英文:

Bidirectional relationship between JPA entities in a hierarchy

问题

在使用Spring Data JPA(以Hibernate为提供者)进行项目开发时,关于何时需要在实体之间建立双向映射的问题让我产生了疑问。

在我的用例中,我有一个名为CategoryGroup的实体,它与Department实体存在@ManyToOne关系。目前,在Department实体中并没有对应的@OneToMany关系。根据我对其他帖子的了解,除非需要通过实体/ Hibernate 来执行删除操作,否则可能不需要建立双向关系。我想知道建立双向关系还能带来什么其他好处?

这是我的Department实体,不包括访问器方法:

@Entity
@Table(name="dbo.Department")
public class Department 
{
    @Id
    @Column(name="id")
    private long id;
    
    @Column(name="name")
    private String name;
}

这是CategoryGroup实体,不包括访问器方法:

@Entity
@Table(name="dbo.CategoryGroup")
public class CategoryGroup
{
    @Id
    @Column(name="id")
    private long id;
    
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="dept_id")
    private Department department;
    
    @Column(name="name")
    private String name;
    
    public CategoryGroup()
    {  		
    }
    
    public CategoryGroup(String name, Department department)
    {
        this.name = name;
        this.department = department;
    }
}

作为后续问题,我的结构具有层次性。Category实体与CategoryGroup实体存在@ManyToOne关系,而SubCategory实体与Category实体存在@ManyToOne关系。

如果我继续在关系的父端添加双向关系映射,是否意味着一旦我检索到Department,我将获得一直到SubCategory的整个实体层次结构?对于我的用例来说,这可能是不可取的。将关系标记为FetchType.LAZY是否可以解决这个问题?

谢谢。

英文:

I am using Spring Data JPA (with hibernate as the provider) for my project and wondering when do I need a bidirectional mapping between entities?

In my use case, I have a CategoryGroup entity having a @ManyToOne relationship to a Department entity. Currently, I don't have a corresponding @OneToMany relation in the Department entity. From what I could understand looking at other posts unless I need to delete via my entity/hibernate, I probably don't need to do it. I am wondering what else do I get by having a bidirectional relationship?

This is my Department excluding the accessors:

@Entity
  @Table(name="dbo.Department")
  public class Department 
  {
    	@Id
       	@Column(name="id")
       	private long id;
        	
       	@Column(name="name")
       	private String name;
  }

This is CategoryGroup excluding accessors:

@Entity
@Table(name="dbo.CategoryGroup")
public class CategoryGroup
{
	@Id
	@Column(name="id")
	private long id;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="dept_id")
	private Department department;
	
	@Column(name="name")
	private String name;
	
	public CategoryGroup()
	{  		
	}
	
	public CategoryGroup(String name, Department department)
	{
		this.name = name;
		this.department = department;
	}
}

As a follow up, my structure is hierarchical in nature. Category entity has a @ManyToOne relationship to a CategoryGroup entity and further on SubCategory entity has a @ManyToOne relationship to a Category entity.

If I keep adding bidirectional relation mapping on the parent side of the relationship then would that mean that once I retrieve the Department I will end up getting the whole entity hierarchy right upto SubCategory? For my use case that won't be desirable. Will marking the relationship with FetchType.LAZY alleviate that?

Thanks

答案1

得分: 1

OneToMany和ManyToMany关联默认情况下是延迟加载的,因此当您添加这些关联时,不会获取整个层次结构。

一般来说,添加这些关联可以在查询方面帮助您,因为它减少了基于连接条件的显式实体连接的需求。除此之外,如果需要,您可以定义级联删除,即如果删除一个部门,您可能希望删除与其关联的所有CategoryGroups。

除了这些,从建模角度来看,明确地将领域分开可能是可取的。您可能希望一个部门是隔离的,即不了解与其连接的对象。

英文:

OneToMany and ManyToMany associations are lazy by default, so you will not fetch the whole hierarchy when you add these associations.

Generally, adding these associations can greatly help you with querying as it reduces the need for explicit entity joins based on the join condition. In addition to that, you can define delete cascading if you need that i.e. if a Department is deleted you might want to delete all CategoryGroups associated with it.

Apart from all that, it might be desireable to explicitly separate domains from a modelling point of view. You might want a Deparment to be isolated i.e. don't know anything about the objects that are connected to it.

huangapple
  • 本文由 发表于 2020年8月18日 07:11:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63459769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定