Issue
I am trying to use an entity as DTO. Here I am setting data using setter to the object but when I fetch data then query executes which tries to fetch the set object which is unsaved in database The following is my code . Setting the data in MailJob and Mail Queue here I am receiving a comma separated list of email values :
List<String> emailList = Arrays.asList(mailQueueDto.getToMail().split(","));
List<MailQueue> mailQueues = new ArrayList<>();
MailJobs mailJobs = new MailJobs();
mailJobs.setFromEmail(mailQueueDto.getFromEmail());
mailJobs.setMailMessage(mailQueueDto.getMailMessage());
mailJobs.setSubject(mailQueueDto.getSubject());
mailJobs.setPassword(mailQueueDto.getPassword());
mailJobs.setId(null);
for (String en : emailList) {
String[] parts = en.split(":");
MailQueue m = new MailQueue();
m.setEmail( parts[0]);
m.setName( parts[1]);
m.setMailJobs(mailJobs);
mailQueues.add(m);
}
mailSchedulerService.pushQueue(mailQueues);
Now pushQueue method in which I try to fetch the same set data. I am doing this for re usability. As soon as the object it fetch using get it tries to merge to data base a query execute which is not required here.
public void pushQueue( List<MailQueue> mailQueue) throws MessagingException {
logger.info("Mail Q--->"+mailQueue);
Session session = getSession();
Transport t = session.getTransport();
t.connect();
try {
for (MailQueue mQ : mailQueue) {
//logger.debug("Market->>"+ mQ );
if(mQ.getEmail().isEmpty())
continue;
Message m = getMailMessage( session,
mQ.getMailJobs().getSubject(),
mQ.getMailJobs().getMailMessage().replace("%name%",mQ.getName()),
mQ.getMailJobs().getFromEmail(),mQ.getEmail());
t.sendMessage(m, m.getAllRecipients());
mailQueueService.deleteById(mQ.getId());
}
}
catch (Exception e){
e.printStackTrace();
}
finally {
t.close();
}
}
Following exception is throw as:
org.springframework.dao.EmptyResultDataAccessException: No class > MailQueue entity with id 0 exists!
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.lambda$deleteById$0(SimpleJpaRepository.java:176)
at java.util.Optional.orElseThrow(Optional.java:290)
at org.springframework.data.jpa.repository.support.Simp
Mail Queue Entity
package com.iitraa.alumnus.iitAlumnus.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import javax.persistence.*;
@Data
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class MailQueue {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String email;
@ManyToOne( cascade = CascadeType.ALL ,fetch = FetchType.LAZY)
private MailJobs mailJobs;
private boolean complete;
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setMailJobs(MailJobs mailJobs) {
this.mailJobs = mailJobs;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
@JsonIgnore
public MailJobs getMailJobs() {
return mailJobs;
}
public int getId() {
return id;
}
public boolean isComplete() {
return complete;
}
public void setComplete(boolean complete) {
this.complete = complete;
}
}
Basically I am trying to use this entity as POJO and its getter and setter to set and then get data without saving but what the query is executed and the error is thrown. How to prevent this?
Solution
Remove the following code as it may invoke and delete the related object in database
mailQueueService.deleteById(mQ.getId());
Answered By - Lucky