gpt4 book ai didi

java - JPA ManyToMany -关系 - joinTable 未填充

转载 作者:行者123 更新时间:2023-11-30 06:32:22 27 4
gpt4 key购买 nike

我的连接表将不会被填充,这是我的类(class):

@Entity
@Table(name = "RECIPIENT")
public class Recipient implements Serializable {

@ManyToMany(targetEntity = Email.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "EMAIL_RECIPIENT", joinColumns = {@JoinColumn(name = "RECIPIENT_ID", nullable = false, updatable = false)},
inverseJoinColumns = {@JoinColumn(name = "EMAIL_ID", nullable = false)})
@Fetch(FetchMode.SELECT)
private Set<Email> emails;

}

@Entity
@Table(name = "EMAIL")
public class Email implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "EMAIL_ID")
private Integer emailId;

@ManyToMany(targetEntity = Recipient.class, mappedBy = "emails")
private Set<Recipient> recipients;

}

在我的服务类中,我通过以下方式保存数据:

public void saveEmail(EmailDTO emailDTO) {
//Save to the db
final Email myEmail = new Email();
myEmail .copyFrom(emailDTO);
for (Recipient recipient : myEmail.getRecipients()) {
recipient.getEmails().add(myEmail );
recipientRepository.save(recipient);
}
recipientRepository.flush();
emailRepository.save(myEmail );
emailRepository.flush();
}

现在我的连接表如下所示:

 Email-ID       Recipient_ID
67891 test@domain.com

它只进行更新..连接表应如下所示:

 Email-ID       Recipient_ID
12343 test@domain.com
54678 test@domain.com
67891 test@domain.com

编辑

我错了,它没有进行更新。我在日志文件中看到它删除了插入的行。例如,如果在连接表中会有这样的行:

Email-ID    : 1234  , 
Recipient_ID:test@domain.com

然后它只是删除这一行并添加新的电子邮件 ID,例如:

Email-ID    : 5678  Recipient_ID:test@domain.com

这是在日志中,首先删除:

delete from email_recipient where recipient_id=? and email_id=?

然后插入:

insert into email_recipient (recipient_id, email_id) values (?, ?)

最佳答案

这是一种双向关系,这意味着关系的每一方都应该有对另一方的引用,因此除了您的电子邮件有一个收件人列表之外,每个收件人还需要有一个电子邮件列表,所以我建议要初始化recipent内的电子邮件列表,您的Recipient类应该如下所示

@Entity
@Table(name = "RECIPIENT")
public class Recipient implements Serializable {

@ManyToMany(targetEntity = Email.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "EMAIL_RECIPIENT", joinColumns = {@JoinColumn(name = "RECIPIENT_ID", nullable = false, updatable = false)},
inverseJoinColumns = {@JoinColumn(name = "EMAIL_ID", nullable = false)})
@Fetch(FetchMode.SELECT)
private List<Email> emails = new ArrayList<>()
...getter setters
}

现在,在迭代Recipent时,将电子邮件添加到每个recipent的电子邮件列表中

 List<Recipient> recipientList = new ArrayList<Recipient>(email.getRecipients());
email.setRecipients(new ArrayList<Recipient>());
for (Recipient recipient : recipientList) {
recipient.getEmails().add(email);
email.getRecipients().add(email);
Recipient persistedRecipient = recipientRepository.save(recipient);

email = persistedRecipient.getEmails().get(persistedRecipient.getEmails().size()-1);//get the managed email
}

另一种方法是执行相同的操作(将电子邮件添加到收件人)并将级联更改为在电子邮件中并保存电子邮件。

@Entity
@Table(name = "RECIPIENT")
public class Recipient implements Serializable {

@ManyToMany(targetEntity = Email.class)
@JoinTable(name = "EMAIL_RECIPIENT", joinColumns = {@JoinColumn(name = "RECIPIENT_ID", nullable = false, updatable = false)},
inverseJoinColumns = {@JoinColumn(name = "EMAIL_ID", nullable = false)})
@Fetch(FetchMode.SELECT)
private Set<Email> emails = new HashSet<>();

}

@Entity
@Table(name = "EMAIL")
public class Email implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "EMAIL_ID")
private Integer emailId;

@ManyToMany(targetEntity = Recipient.class, mappedBy = "emails"وcascade = CascadeType.ALL)
private Set<Recipient> recipients = new HashSet<>();

}

在你的 saveEmail 方法中

public void saveEmail(EmailDTO emailDTO) {
//Save to the db
final Email myEmail = new Email();
myEmail .copyFrom(emailDTO);
for (Recipient recipient : myEmail.getRecipients()) {
recipient.getEmails().add(myEmail );

}

emailRepository.save(myEmail );

}

关于java - JPA ManyToMany -关系 - joinTable 未填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45845316/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com