Это мои сущности;
@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;
}
это метод службы, который я пытаюсь удалить:
@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);
}
он работает нормально, когда я удаляю этот блок в методе службы;
if (merchantApplication.getApplication().getIpAddresses().size() == 1) {
throw new IpAddressCannotBeDeleted();
}
Но так, это не работает. Можете ли вы помочь мне, в чем проблема?