![](/img/trans.png)
[英]How do I delete orphan entities using hibernate and JPA on a many-to-many relationship?
[英]How to do many-to-many relationship without deleting the previous data in mapping table using Hibernate or JPA
我有 2 個實體: User.java和PushNotification.java和名為userpushnotification的映射表,其中發生多對多映射。 userpushnotification 表中的現有數據對我來說很重要。 因此,如果我嘗試為推送通知(id=2)添加用戶(比如說 id=5,6,7), hibernate會刪除關系表中推送通知(id=2)的先前數據,然后為此添加新用戶推送通知(id=2)。
我需要將所有記錄保存在關系表中。 那么我如何限制 Hibernate/JPA 只執行插入查詢而不是執行刪除和插入查詢。 簡而言之,我只想將 append 數據放在關系表中,而不是覆蓋。
用戶.java:-
@Entity
@Table(name = "user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "id")
@GenericGenerator(name = "gen", strategy = "identity")
@GeneratedValue(generator = "gen")
private long id;
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
@Column(name = "authkey")
private String authKey;
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "users")
private Set<PushNotifications> pushNotifications = new HashSet<PushNotifications>();
PushNotifications.java:-
@Entity
@Table(name = "pushnotifications", uniqueConstraints = @UniqueConstraint(columnNames = { "id" }))
public class PushNotifications implements Serializable {
@Id
@Column(name = "id")
@GenericGenerator(name="gen",strategy="identity")
@GeneratedValue(generator="gen")
private long id;
@Column(name = "shortdescription")
private String shortDescription;
@Column(name = "title")
private String title;
@JsonIgnore
@ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
@JoinTable(name = "userpushnotifications",
joinColumns = {@JoinColumn(name = "pushnotificatoinId") },
inverseJoinColumns = { @JoinColumn(name = "userId") })
private Set<User> users = new HashSet<User>();
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
當我嘗試這樣做時:
PushNotifications notifications = iNotificationService
.getNotification(notificationId);
Set<User> newUsers = new HashSet<User>();
newUsers .add(newUserToBeNotified_1);
newUsers .add(newUserToBeNotified_2);
notifications.setUsers(newUsers);
sessionFactory.getCurrentSession().merge(notifications);
在這里,我想為該通知類型添加 2 個用戶,在關系表中已經有一個用於該通知類型的用戶。 Hibernate 執行這些查詢:
Hibernate:
delete
from
userpushnotifications
where
pushnotificatoinId=?
and userId=?
Hibernate:
insert
into
userpushnotifications
(pushnotificatoinId, userId)
values
(?, ?)
Hibernate:
insert
into
userpushnotifications
(pushnotificatoinId, userId)
values
(?, ?)
所以,我希望你明白我,我不希望 hibernate 進行刪除操作。 請幫我解決這個問題,尋找答案......在此先感謝。
您可以為此使用Blaze-Persistence ,它在 JPA 之上工作,並為 collections 的 DML 操作提供支持。 查詢可能如下所示
criteriaBuilderFactory.insertCollection(entityManager, PushNotifications.class, "users")
.fromIdentifiableValues(User.class, "u", newUsers)
.bind("id", notification.getId())
.bind("users").select("u")
.executeUpdate();
按照文檔中的描述設置 Blaze-Persistence 后,您可以創建如下存儲庫:
@Component
class MyRepository {
@Autowired CriteriaBuilderFactory cbf;
@Autowired EntityManager em;
public void addUsers(Collection<User> newUsers) {
cbf.insertCollection(em, PushNotifications.class, "users")
.fromIdentifiableValues(User.class, "u", newUsers)
.bind("id", notification.getId())
.bind("users").select("u")
.executeUpdate();
}
}
這將只向連接表發出插入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.