How to get emailId From User table in the OrderServiceImpl.java class so that it can be sent as a String while calling sendTextEmail(String email) in OrderServiceImpl.java class?
User.java Class
@Setter
@Getter
@EqualsAndHashCode
@ToString
@NoArgsConstructor
//@AllArgsConstructor
@Entity
@Table(name = "`user`", uniqueConstraints = {@UniqueConstraint(columnNames = "email")})
public class User {
public User(String email, String name, String password, String address) {
this.email = email;
this.name = name;
this.password = password;
this.address = address;
}
@Id
@Column(name = "userId")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Size(max = 50)
@Email
private String email;
@Size(max = 50)
@NotBlank
private String name;
@Size(max = 100)
@NotBlank
private String password;
@Size(max = 50)
@NotBlank
private String address;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "cart", joinColumns = @JoinColumn(name = "userId"),
inverseJoinColumns = @JoinColumn(name = "foodId"))
private List<Food> cart = new ArrayList<Food>();
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "user_roles", joinColumns = @JoinColumn(name = "userId"),
inverseJoinColumns = @JoinColumn(name = "roleId"))
private Set<Role> roles = new HashSet<Role>();
}
Order.java Class
@Setter
@Getter
@EqualsAndHashCode
@ToString
@NoArgsConstructor
//@AllArgsConstructor
@Entity
@Table(name = "orders", uniqueConstraints = {@UniqueConstraint(columnNames = "orderTransaction")})
public class Order {
@Id
@Column(name = "orderId")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Size(max = 50)
@NotBlank
private String orderTime;
@Size(max = 50)
@NotBlank
private String orderTransaction;
@OneToMany(fetch = FetchType.LAZY)
@JoinTable(name = "users_orders", joinColumns = @JoinColumn(name = "orderId"),
inverseJoinColumns = @JoinColumn(name = "userId"))
private List<Order> orders = new ArrayList<Order>();
}
EmailServiceImpl.java Class
@Service
public class EmailServiceImpl implements EmailService {
private static final Logger logger = LoggerFactory
.getLogger(EmailServiceImpl.class);
@Autowired
private JavaMailSender javaMailSender;
@Override
//Sending User Object
public void sendTextEmail(String email) {
logger.info("Simple Email sending start");
SimpleMailMessage simpleMessage = new SimpleMailMessage();
simpleMessage.setTo(email); //Enter customer email
simpleMessage.setSubject("Spring Boot=> Sending simple email");
simpleMessage.setText("Dear hunger buddy customer Hope you are doing well.");
javaMailSender.send(simpleMessage);
logger.info("Simple Email sent");
}
}
OrderServiceImpl.java Class
@Service
public class OrderServiceImpl implements OrderService{
private static final String String = null;
@Autowired
private EmailService emailService;
@Autowired
private OrderRepo orderRepo;
/*@Autowired
private User register;*/
@Override
public Order addOrder(Order order) throws AlreadyExistsException {
if(orderRepo.existsById(order.getId())) {
throw new AlreadyExistsException("This record already exists");
}
else
{
//long temp=order.getId();
//Optional<User> optional = userRepo.findById(temp);
String email = null;
emailService.sendTextEmail(email);
return orderRepo.save(order);
}
}
@Override
public Optional<List<Order>> getAllOrder() {
return Optional.ofNullable(orderRepo.findAll());
}
@Override
public Optional<Order> getOrderById(Long id) throws IdNotFoundException {
Optional<Order> optional = orderRepo.findById(id);
if (!optional.isPresent()) {
throw new IdNotFoundException("Sorry Order Not Found");
}
return optional;
}
}
I have removed all the import statement to save the time of reader.
ThankYou
CodePudding user response:
I think you have a logical issue in Order
entity definition. Order
does not contain another list of Orders. But it should contain the User
who placed the order. Hence, the definition should be corrected as follows:
@Setter
@Getter
@EqualsAndHashCode
@ToString
@NoArgsConstructor
@Entity
@Table(name = "orders", uniqueConstraints = {@UniqueConstraint(columnNames = "orderTransaction")})
public class Order {
@Id
@Column(name = "orderId")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Size(max = 50)
@NotBlank
private String orderTime;
@Size(max = 50)
@NotBlank
private String orderTransaction;
@OneToOne(fetch = FetchType.LAZY)
@JoinTable(name = "users_orders", joinColumns = @JoinColumn(name = "orderId"),
inverseJoinColumns = @JoinColumn(name = "userId"))
private User user;
}
Then in OrderServiceImpl
you could access user information with following:
User user = order.getUser();
String emailId = user.getEmail();