@Parent Initialization not done at persist (Done on loading)

I took the example from the documentation :
https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#embeddable-Parent

But what I understand is that the initialization of the @Parent variable is only performed during the load.

Because if we remove the line from entityManager.clear() it keeps the same instance and therefore the @Parent is null and the test is in error

public class TestITParent {

  @Test
  public void tester_parent() {
    EntityManager entityManager = Bootstrap.createEntityManager("archi");
    entityManager.getTransaction().begin();

    City cluj = new City();
    cluj.setName("Cluj");
    cluj.setCoordinates(new GPS("46", "23"));

    entityManager.persist(cluj);
    entityManager.flush();
    entityManager.clear(); // If commented the test is in error

    City cluj2 = entityManager.find(City.class, cluj.getIdAsString());
    Assertions.assertEquals(cluj2, cluj2.getCoordinates().getCity());

    entityManager.getTransaction().rollback();
    entityManager.close();
  }
}

@Entity(name = "City")
@Table(name="TCONTRAT")
public class City extends HermesBusinessObject {

  @Column(name="prenomsignataire")
  private String name;

  @Embedded
  private GPS coordinates;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public GPS getCoordinates() {
    return coordinates;
  }

  public void setCoordinates(GPS coordinates) {
    this.coordinates = coordinates;
  }
}

@Embeddable
public class GPS {

  public GPS() {
  }

  public GPS(String latitude, String longitude) {
    this.latitude = latitude;
    this.longitude = longitude;
  }

  @Column(name="intitulecotitulaire")
  private String latitude;
  @Column(name="intitulesignataire")
  private String longitude;
  @Parent
  private City city;

  public String getLatitude() {
    return latitude;
  }

  public void setLatitude(String latitude) {
    this.latitude = latitude;
  }

  public String getLongitude() {
    return longitude;
  }

  public void setLongitude(String longitude) {
    this.longitude = longitude;
  }

  public City getCity() {
    return city;
  }

  public void setCity(City city) {
    this.city = city;
  }
}

That’s correct. I don’t think that any interaction with the object model should happen in this case though, so IMO this is not a bug.

1 Like