Spring boot 3.1.1 - Hibernate 6.2.5.Final
Embedded field are not correctly instantiated when I use entity is used in a composite key to load my entity
{noformat}@Entity @Table(name = "trip") public class Trip {
@Id public Long id;
@Embedded @AttributeOverrides({ @AttributeOverride(name = "date", column = @Column(name = "date_position1")), }) @AssociationOverride(name = "poi", joinColumns = @JoinColumn(name = "poi1_id_fk")) public Location position1 = new Location();
@Embedded @AttributeOverrides({ @AttributeOverride(name = "date", column = @Column(name = "date_position2")), }) @AssociationOverride(name = "poi", joinColumns = @JoinColumn(name = "poi2_id_fk")) public Location position2 = new Location(); }{noformat}
in certain scenarios (see my code attached), trip.position2 is null when it shouldn't be ({{poi2_id_fk}} is null but not {{date_position2}})
scenario 1 (working)
{noformat} Trip trip = tripRepository.findById(1L).orElseThrow(); assertThat(trip.position1).isNotNull(); assertThat(trip.position2).isNotNull();{noformat}
scenario 2 (not working)
{noformat} @Entity @Table(name = "report_trip") public class ReportTrip {
@EmbeddedId public ReportTripId compositeKey = new ReportTripId();
@Column(name = "other") public String other; }
@Embeddable public class ReportTripId implements Serializable {
@ManyToOne @JoinColumn(name = "report_id_fk") public Report report;
@ManyToOne @JoinColumn(name = "trip_id_fk") public Trip trip;
}{noformat}
{noformat} Optional<Report> reportOptional = repository.findById(1L); assertThat(reportOptional).isPresent();
Report report = reportOptional.get(); assertThat(report.reportTripList.get(0).compositeKey.trip.position1).isNotNull(); assertThat(report.reportTripList.get(0).compositeKey.trip.position2).isNotNull(); // ko{noformat} |
|