These are my entities;
@Entity
public class IpAddress{
@Id
@Column(nullable = false, updatable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ")
@Getter
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private final Application application;
.....
}
@Entity
public class Application{
@Column(nullable = false, updatable = false)
private final String applicationId;
@OneToMany(mappedBy = "application", cascade = CascadeType.ALL, orphanRemoval = true)
private List<IpAddress> ipAddresses = new ArrayList<>();
}
@Entity
public class MerchantApplication {
@Id
@Column(nullable = false, updatable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ")
@Getter
private Long id;
@OneToOne
private final Merchant merchant;
@OneToOne(cascade = CascadeType.ALL)
private final Application application;
@ManyToOne(fetch = FetchType.LAZY)
private final User user;
}
this is the service method that I try to delete:
@Transactional
public void deleteIpAddressForMerchant(Long merchantId,Long ipAddressId) {
Merchant merchant = merchantRepository.getMerchant(merchantId);
MerchantApplication merchantApplication = merchant.getMerchantApplication();
if (Objects.isNull(merchantApplication))
throw new ApplicationNotFoundException();
if (merchantApplication.getApplication().getIpAddresses().size() == 1) {
throw new IpAddressCannotBeDeleted();
}
IpAddress ipAddress = ipAddressRepository.getByIdAndApplication(ipAddressId, merchantApplication.getApplication());
ipAddressRepository.delete(ipAddress);
}
it works fine when i remove this block in service method;
if (merchantApplication.getApplication().getIpAddresses().size() == 1) {
throw new IpAddressCannotBeDeleted();
}
But this way, it's not work.Can you help me , what is the problem ?