gpt4 book ai didi

java - 我正在 hibernate 状态下保存一个实体。它正在更新同一语句中已保存的数据。如何解决这个问题?

转载 作者:太空宇宙 更新时间:2023-11-04 13:42:49 26 4
gpt4 key购买 nike

我有两个实体父实体和子实体:
Group_Email_History.java(父级)

@Entity
@Table(name = "Group_Email_History")
public class Group_Email_History implements Serializable, Cloneable{

@Id
@GeneratedValue()
@Column(name = "ID")
private Integer id;

@ManyToOne
@JoinColumn(name = "GroupId", nullable = false)
private Groups groups;

@OneToMany(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name="GROUP_EMAIL_HISTORY_ID",nullable=true)
@JsonIgnore
private Set<AttachmentInfo> attachmentInfo = new HashSet<AttachmentInfo>(0);



@ManyToOne
@JoinColumn(name = "UserId", nullable = false)
private Users users;

@NotEmpty

@Column(name = "EmailId")
private String emailId;

@Column(name = "Subject")
private String subject;

@Column(name = "EmailText")
private String emailText;
@NotEmpty

@Column(name = "EmailSentDate")
private String emailSentDate;

@Column(name = "isEmailSent")
private boolean isEmailSent;


@Transient
private String attachedFileName;

@Transient
private List<MultipartFile> alist;
//setters and getters

AttachmentInfo.java(子):

@Entity
@Table(name="AttachmentInfo")
public class AttachmentInfo implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
private Integer attachment_Id;

@Column(name="attachedFileName")
private String attachedFileName;

@Column(name="attachedFileSize")
private Long attachedFileSize;

@Column(name="attachedFile")
@Lob
private byte[] attachedFile;


@Column(name="attachedFileType")
private String attachedFileType;


@Transient
private String attachedFilePath;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "UserId", nullable = false)
private Users users;



@ManyToOne(fetch=FetchType.EAGER,cascade = CascadeType.ALL)
@JoinColumn(name = "GROUP_EMAIL_HISTORY_ID",nullable=true,insertable=false,updatable=false)
private Group_Email_History group_Email_History;


@ManyToOne(fetch=FetchType.EAGER,cascade = CascadeType.ALL)
@JoinColumn(name = "SCHEDULE_ID",nullable=true)
private Schedule_Email schedule_Email;

现在我正在保存父实体..它还将通过调用来保存子实体:

sessionFactory.getCurrentSession().save(parent);

调用上面的 save() 后,控制台是:

我正在附件表中保存多个附件..
但问题是附件表具有 groupemailhisory 表主键,例如:

attachment_id    group_email_history_id    ........
1 1 ........etc

首先将 group_email_history_id 更新为 1。
之后 save() 调用 group_email_history_id 值并将其更新为对象中可用的最新值。即

attachment_id    group_email_history_id    ........  
1 2 ........
2 2 ........

注意:如果在一次 save() 内将多个 group_email_history_id 保存在 group_Email_History 表中,就会发生这种情况。
如何解决这个问题?请帮助我......

我的 Controller 代码是:

@RequestMapping(value= "/groupEmail",method = RequestMethod.POST)
public String doGroupEmail(@ModelAttribute("email") @Valid Group_Email_History email,BindingResult result) {
try{
//date object creation
Date date = new Date();
//formating the date
SimpleDateFormat df2 = new SimpleDateFormat("dd/MMM/yyyy HH:mm");
String formattedDate = df2.format(date);
email.setEmailSentDate(formattedDate);

HashSet<AttachmentInfo> hs = new HashSet<AttachmentInfo>();

List<MultipartFile> alist = files;

long size =0;






// creates Mime message Object
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true,"UTF-8");

messageHelper.setSubject(email.getSubject());
messageHelper.setText(email.getEmailText());

String[] emailids = email.getEmailId().split(",");

//iterating for mail Ids
for(MultipartFile mpf : alist){
final MultipartFile mp = mpf;
size+=mp.getSize();

InputStreamSource iss = new InputStreamSource() {
@Override
public InputStream getInputStream() throws IOException {
// provide fresh InputStream
return mp.getInputStream();
}
};
messageHelper.addAttachment(mp.getOriginalFilename(), iss);
}
for(String mailid : emailids)
{

if(mailid != ""){
final Integer userid = iGroupEmailService.getUserId(mailid);

for(MultipartFile mpf : alist){
final MultipartFile mp = mpf;
size+=mp.getSize();


hs.add(new AttachmentInfo(){

{
setAttachedFile(mp.getBytes());
setAttachedFileName(mp.getOriginalFilename());
setAttachedFileSize(mp.getSize());
setAttachedFileType(mp.getContentType());
setUsers(new Users(){{
setUserId(userid);
}});

}});
}
email.setAttachmentInfo(hs);
Group_Email_History email2 = (Group_Email_History)email.clone();
email2.setEmailId(mailid);
email2.setUsers(new Users(){{
setUserId(userid);
}});

iGroupEmailService.saveEmailSendingInformation(email2);

messageHelper.setTo(mailid);
// sends the e-mail
mailSender.send(message);
}

}

最佳答案

首先,我发现理解你的帖子确实有问题。不过,据我所知,您在不需要的更新语句方面遇到了一些麻烦。您应该尝试更改这部分:

@OneToMany(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name="GROUP_EMAIL_HISTORY_ID",nullable=true)
@JsonIgnore
private Set<AttachmentInfo> attachmentInfo = new HashSet<AttachmentInfo>(0);

进入此:

@OneToMany(mappedBy = group_Email_History, cascade=CascadeType.ALL,
fetch = FetchType.EAGER)
@JsonIgnore
private Set<AttachmentInfo> attachmentInfo = new HashSet<AttachmentInfo>(0);

因此,在子实体上,您的代码应如下所示:

@ManyToOne(fetch=FetchType.EAGER,cascade = CascadeType.ALL)
@JoinColumn(name = "GROUP_EMAIL_HISTORY_ID",nullable=true)
private Group_Email_History group_Email_History;

@ManyToOne 通常是关系的所有者,并用 @JoinColumn 进行注释。因此,@OneToMany 获取 mappedBy 属性。了解更多 here .

负责持久化的代码应如下所示:

attachmentInfo.add(attachement1);
attachmentInfo.add(attachement2);
group_Email_History.setAttachementInfo(attachmentInfo);
attachement1.setGroup_Email_History(group_Email_History);
attachement2.setGroup_Email_History(group_Email_History);
...
sessionFactory.getCurrentSession().save(group_Email_History);

编辑:尝试更改这部分:

hs.add(new AttachmentInfo(){
{
//you need to set Group_Email_History object
//to every AttachmentInfo Object as well
setGroup_Email_History(email);

setAttachedFile(mp.getBytes());
setAttachedFileName(mp.getOriginalFilename());
setAttachedFileSize(mp.getSize());
setAttachedFileType(mp.getContentType());
setUsers(new Users(){{
setUserId(userid);
}});
}});

关于java - 我正在 hibernate 状态下保存一个实体。它正在更新同一语句中已保存的数据。如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31104564/

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