Java / Hibernate - 如何禁用已加入的实体更新

时间:2018-05-24 06:56:31

标签: java spring hibernate jpa persistence

我想通过entityManager.persist(myEntity)将实体bellow保存到数据库。但是,孩子的(ProjectEntity)孩子也会更新。如何禁用子实体更新的更新。 谢谢。

@Entity
@Table(name = "my_entity")    
public class MyEntity {
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE)         
        private Integer id;

        @Column
        private String user;

        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "project_id")
        private ProjectEntity project;
}

1 个答案:

答案 0 :(得分:0)

请在您的实体中更新以下代码

@ManyToOne(fetch = FetchType.LAZY,orphanRemoval=true)
@JoinColumn(name = "project_id")
private ProjectEntity project;