我有兩個實體是這樣的:JPA如何避免更新相關的實體@ManyToOne或者@OneToOne
@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;
}
通知和NotificationTarget相關聯,如果我更新NotificationTarget
:
NotificationTarget notificationTarget = notificationTargetRepository.findByNotificationIdAndUserId(
notificationId, userId);
notificationTarget.setUserId(userId);
notificationTargetRepository.save(notificationTarget);
Hibernate會也更新Notification
。 我檢查過更新通知,因爲通知有EntityListener,當DefaultFlushEntityEventListener
調用攔截器時,AuditingEntityListener
將更改updatedAt
字段。但在這個商業案例中,我不想在更新NotificationTarget時更改通知,是否有一些建議?
對不起,我認爲問題描述是錯誤的。
我調試過,發現因爲NotificationLang
列表被檢查爲CollectionType.isEqual
髒。但我不知道它爲什麼很髒?
你能解釋一下你最後一段嗎?你不想更新NotificationTarget中的通知嗎?如果你想更新通知,你想在哪些情況下更新它? – XtremeBaumer
'Notifiaction'改變了什麼? –
@XtremeBaumer我認爲總是不想更新NotificationTarget中的Notification,它只是爲了select。 – xiaosunzhu