cascade = cascadeType和fetch = FetchType的用法 [英] Usage of cascade=cascadeType and fetch = FetchType

查看:217
本文介绍了cascade = cascadeType和fetch = FetchType的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JPA 2。



我想知道以下内容。


  • 当父表中不存在
    父键时,限制插入,更新或删除操作的最佳方法是什么。



    下面的实体场景

      @Entity 
    public class Employee {
    @OneToMany(mappedBy =requester )
    私人设定< Project> requestedProjects;

    @OneToMany(mappedBy =批准者)
    私人设置< Project> approvedProjects;
    }

    @实体
    公共类项目
    @ManyToOne
    @JoinColumn(name =EMPLOYEE_NUMBER,referencedColumnName =EMPLOYEE_NUMBER)
    私人雇员请求者;

    @ManyToOne
    @JoinColumn(name =APPROVER,referencedColumnName =EMPLOYEE_NUMBER)
    私人员工批准者;
    }




对这个问题的理解是使用 cascade = cascadeType.ALL 如果父键不存在则插入和删除。



MERGE PERSIST ALL




  • 除了建议使用 fetch = FetchType



任何帮助都非常可观

解决方案

这些是单独的问题。
1)级联设置按顾名思义:当您调用entityManager持久化时,合并或移除它会对传入的实体执行操作。然后根据映射设置将此调用级联到参考实体,就好像您在引用的实体上显式调用该方法。因此,Project->请求者,如果你调用persist在Project上,它会调用persist在项目和请求者员工上,如果级联持续或指定了所有类型的话。因此,如果员工存在但被分离,它将立即或根据您的提供者进行刷新/提交,就像直接调用em.persist(employee)一样。合并和移除按照JPA规范概述的自己的限制和行为以相同的方式运行。 CascadeType.All只是列出所有级联类型的简便方式:REMOVE,PERSIST和MERGE,而不必输入它们。

You应该在有意义时使用它们,而不是将每个关系标记为CascadeType.ALL。有很多教程可以提供帮助。


2)提取类型决定了提取何时发生。通常推荐使用懒惰,这就是为什么它是JPA规范中默认的集合映射,因为不必要地引入对象会浪费资源。但这一切都取决于应用程序的使用模式,以及如何优化提供程序,数据库和映射。使用默认值,读取项目不会导致读入所有引用的员工,直到触发收集。但在员工中阅读将导致其项目从数据库中读入。应该注意的是,通过阅读单个实体实例,您不会错误地读取整个数据库 - 如果您有渴望的集合映射,这是很常见的。

I am using JPA 2.

I would like to know the following.

  • What is the best approach to restrict insert, update or delete when parent key does not exist in parent table.

    I have the following Entity scenario

       @Entity
       public class Employee {
       @OneToMany(mappedBy = "requester")
       private Set<Project> requestedProjects;
    
       @OneToMany(mappedBy = "approver")
       private Set<Project> approvedProjects;
       }
    
       @Entity
       public class Project
       @ManyToOne
       @JoinColumn(name = "EMPLOYEE_NUMBER", referencedColumnName = "EMPLOYEE_NUMBER")
       private Employee requester;
    
       @ManyToOne
       @JoinColumn(name = "APPROVER", referencedColumnName = "EMPLOYEE_NUMBER")
       private Employee approver;
       }
    

What is my limited understanding of this matter is usage of cascade=cascadeType.ALL does insert and delete if parent key doesn't exist.

And what is the difference between MERGE, PERSIST and ALL?

  • Besides when it is advisable to use fetch = FetchType

Any help is highly appreciable

解决方案

These are separate questions. 1) Cascade settings do what the name suggests: when you call entityManager persist, merge or remove it performs the operation on the entity passed in. This call then gets cascaded to referenced entities depending on the mapping settings, as if you explicitly called that method on the referenced entity. So Project->requester, if you call persist on Project, it would call persist on both the project and the requester employee if either cascade persist or all types are specified. So if the employee exists but is detached, it will cause an exception either immediately or on flush/commit depending on your provider, just as if you called em.persist(employee) directly. Merge and remove operate the same way following their own restrictions and behavior outlined by the JPA specification.

CascadeType.All is just a convenient way of listing all the cascade types: REMOVE, PERSIST and MERGE without having to type them out.
You should use them when they make sense, and not just mark every relationship as CascadeType.ALL. There are plenty of tutorials that can help

2) fetch type determines when the fetch occurs. Lazy is usually recommended, which is why it is the default for collection mappings in the JPA specification, as bringing in objects unnecessarily can waste resources. But it all depends on your application's usage patterns, and how you have optimized your provider, database and mappings. Using the defaults, reading in a Project will not cause all referenced Employees to be read in, until the collection is triggered. But reading in an Employee will cause its project to be read in from the database. Care should be taken that by reading in a single entity instance, you do not mistakenly read in the entire database - which is common if you have eager collection mappings.

这篇关于cascade = cascadeType和fetch = FetchType的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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