I'm new to JPA and Hibernate and came across strange behavior. Consider the code below.
License
entity:
@Entity
@Data
public class License {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Enumerated(EnumType.STRING)
private LicenseType type;
@Column(unique = true)
private String activationKey;
@OneToMany(mappedBy = "id", cascade = CascadeType.REMOVE)
private List<Payment> payments = new ArrayList<>();
private long productId;
private String productName;
private long term;
private long creationTimestamp;
private boolean active;
}
LicenceType
enum:
public enum LicenseType {
NAMED
}
Payment
entity:
@Entity
@Data
public class Payment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.REFRESH})
private License license;
private BigDecimal sum;
}
LicenceRepository
:
@Repository
public interface LicenseRepository extends CrudRepository<License, Long> {
}
PaymentRepository
:
@Repository
public interface PaymentRepository extends CrudRepository<Payment, Long> {
}
Bootstrap class:
@SpringBootApplication
public class LsPocApplication {
public static void main(String[] args) {
SpringApplication.run(LsPocApplication.class, args);
}
@Bean
public CommandLineRunner demo(LicenseRepository licenseRepository, PaymentRepository paymentRepository) {
return (args) -> {
License license = new License();
license.setActivationKey(UUID.randomUUID().toString());
Payment payment = new Payment();
payment.setSum(BigDecimal.valueOf(new Random().nextDouble()));
payment.setLicense(license);
paymentRepository.save(payment);
// licenseRepository.delete(license); // This does nothing
// licenseRepository.delete(license.getId()); // This deletes both licence and associated payment(s)
};
}
}
So the question is why the licenseRepository.delete(license.getId())
works as intended, but licenseRepository.delete(license)
does nothing? I assumed, they are logically equivalent. Or I'm wrong?
Please advise.
Thanks in advance!