JPA ManyToMany -relationship - joinTable未填充

时间:2017-08-23 16:48:52

标签: java spring hibernate spring-data-jpa

我的加入表将不会填充, 这是我的课程:

@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

修改

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

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 (?, ?)

1 个答案:

答案 0 :(得分:1)

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

@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时,将电子邮件添加到每个食谱的电子邮件列表

 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 );

}