• CascadeType


    当Hibernate配置了(JPA注解)

    cascade = { CascadeType.PERSIST, CascadeType.MERGE }

    调用保存时

    session.save(user);

    并不能级联保存,因为save不会触发CascadeType.PERSIST。看save的javadoc:

        /**
         * Persist the given transient instance, first assigning a generated identifier. (Or
         * using the current value of the identifier property if the <tt>assigned</tt>
         * generator is used.) This operation cascades to associated instances if the
         * association is mapped with {@code cascade="save-update"}
         *
         * @param object a transient instance of a persistent class
         *
         * @return the generated identifier
         */
        Serializable save(Object object);

    save会触发“save-update”和JPA的CascadeType.ALL,并不会触发CascadeType.PERSIST。JPA没有“save-update”的CascadeType,只能使用Hibernate的CascadeType,如果想保存时触发级联:

    1. 可以使用JPA的CascadeType.ALL

    2. 使用Hibernate的CascadeType.SAVE_UPDATE

    @Cascade(CascadeType.SAVE_UPDATE)

    触发其他JPA的CascadeType类型,请参见org.hibernate.session源码(或者javadoc)中各个方法,比如:

        /**
         * Make a transient instance persistent. This operation cascades to associated
         * instances if the association is mapped with {@code cascade="persist"}
         * <p/>
         * The semantics of this method are defined by JSR-220.
         *
         * @param object a transient instance to be made persistent
         */
        void persist(Object object);

    也就是说该persist方法会触发JPA的CascadeType.PERSIST。

    Hibernate支持的CascadeType类型,请见org.hibernate.annotations.CascadeType源码,或者javadoc

  • 相关阅读:
    创建cube 维度层次
    Apress 出版社电子书
    es 集群部署
    redis集群部署
    kafka 简介
    Hadoop伪集群部署
    activitmq+keepalived+nfs 非zk的高可用集群构建
    docker的安装及基础操作与镜像构建
    docker 创建容器与管理容器
    邮箱服务器搭建
  • 原文地址:https://www.cnblogs.com/drizzlewithwind/p/6195122.html
Copyright © 2020-2023  润新知