本文介绍了请参考JPA @Column注释解释有关insertable = false和updatable = false的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果将字段注释为insertable=false, updatable=false,这是否意味着您不能插入值或更改现有值?您为什么要这样做?

If a field is annotated insertable=false, updatable=false, doesn't it mean that you cannot insert value nor change the existing value? Why would you want to do that?

@Entity
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToMany(mappedBy="person", cascade=CascadeType.ALL)
    private List<Address> addresses;
}

@Entity
public class Address {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne
    @JoinColumn(name="ADDRESS_FK")
    @Column(insertable=false, updatable=false)
    private Person person;
}

推荐答案

当创建/更新相关实体的责任不在当前实体.例如.您有一个PersonAddress.您想将insertable=false, updatable=false添加到与Address实体中的Person实体的@OneToMany关系中,仅仅是因为Address实体不负责创建或更新Person.恰恰相反.

You would do that when the responsibility of creating/updating the related entity in question isn't in the current entity. E.g. you have a Person and an Address. You'd like to add insertable=false, updatable=false to the @OneToMany relationship with the Person entity in the Address entity, simply because it's not the responsibility of the Address entity to create or update a Person. It's the other way round.

这篇关于请参考JPA @Column注释解释有关insertable = false和updatable = false的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 09:00