Java JPA如何避免更新@ManyToOne或@OneToOne中的关联实体

Java JPA如何避免更新@ManyToOne或@OneToOne中的关联实体,java,hibernate,jpa,Java,Hibernate,Jpa,我有两个这样的实体: @Entity @Table(name = "article") @EntityListeners({AuditingEntityListener.class}) public class Notification implements Serializable { @GeneratedValue(strategy = GenerationType.IDENTITY) @Id private Integer id; @OneToMany(

我有两个这样的实体:

@Entity
@Table(name = "article")
@EntityListeners({AuditingEntityListener.class})
public class Notification implements Serializable {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    private Integer id;

    @OneToMany(mappedBy = "notification", cascade = {CascadeType.PERSIST})
    private List<NotificationLang> langs;

    @OneToMany(mappedBy = "notification", cascade = {CascadeType.PERSIST})
    private List<NotificationTarget> targets;

    @LastModifiedDate
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "updated_at")
    private Date updatedAt;

    @CreatedDate
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_at")
    private Date createdAt;

}


@Entity
@Table(name = "article_count")
@EntityListeners({AuditingEntityListener.class})
public class NotificationTarget implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonIgnore
    private Integer id;

    @Column(name = "user_id")
    private String userId;

    @ManyToOne(optional = false)
    @JoinColumn(name = "notification_id")
    private Notification notification;
}
hibernate也将更新通知。 我已选中更新通知,因为通知具有EntityListener,
AuditingEntityListener
将在调用侦听器时更改
updatedAt
字段。但是在这个业务案例中,我不想在更新NotificationTarget时更改通知,有什么建议吗

对不起,我认为问题描述是错误的


我调试并发现更新都是因为
NotificationLang
列表在
CollectionType.isEqual
中被检查为脏的。但我不知道为什么它很脏

您可以在
NotificationTarget

中的映射注释中添加
insertable=false,updateable=false
属性。您已经将cascade={CascadeType.PERSIST}添加到通知类中的目标,这将把持久性操作传播到所有相关的实体管理器


删除它。

你能再解释一下你的最后一段吗?您永远不想更新NotificationTarget中的通知吗?如果您想更新通知,在哪些情况下您想更新它?
Notifiaction
中的哪些内容发生了更改?@XtremeBaumer我想总是不想更新NotificationTarget中的通知,它只是用于选择。@DaveParal在我的代码中,notification中的updatedAt字段发生了更改,因为@EntityListeners({AuditingEntityListener.class})你也可以发布你的AuditingEntityListener类吗?对不起,我已经尝试过了,这不起作用。因为这是合并操作,而不是持久化。@Ankuruse cascade={CascadeType.ALL}对不起,它不起作用,因为可更新的意思是:这个列是否包含在语句中。@anup0513
NotificationTarget notificationTarget = notificationTargetRepository.findByNotificationIdAndUserId(
        notificationId, userId);
notificationTarget.setUserId(userId);
notificationTargetRepository.save(notificationTarget);