Hi Everyone,
I have posted a question in Stack Overflow
but so far no luck.
Here is a copy-paste of the description.
I am trying to get lifecycle related callbacks on Embeddable
beans using Hibernate 5.4.10.Final. I have been able to locate a bug seemingly related to this issue
https://hibernate.atlassian.net/browse/HHH-12326
but it states the problem has been fixed in earlier Hibernate versions and I am definitely still seeing the issue it in collections of Embeddable
.
I took the example from
https://javabydeveloper.com/mapping-collection-of-embeddablecomposite-types-jpa-with-hibernate/
and modified it slightly to illustrate the problem
I put a sample app here
https://github.com/pzieminski/collection-of-embeddables1
create table user
(
id int primary key,
user_name text,
password text,
created_time datetime,
updated_time datetime,
user_type text
);
create table contact_address
(
street_address text,
state text,
city text,
zip_code text,
user_id int references user,
addr_type text
);
create table contact
(
contact_ text,
id int references user
);
Then I have the beans
@Data
@Entity(name = "user")
public class User {
@Id
@Column(name = "id")
private Integer id;
@Column(name = "user_name")
private String userName;
@Column(name = "password")
private String password;
@Embedded
private Audit audit = new Audit();
@ElementCollection
@CollectionTable(name = "contact_address", joinColumns = @JoinColumn(name = "user_id"))
@AttributeOverride(name = "streetAddress", column = @Column(name = "street_address"))
private List<ContactAddress> address;
@ElementCollection
@CollectionTable(name = "contact", joinColumns = @JoinColumn(name = "id"))
@Column(name = "contact no")
private Collection<String> contacts;
@Enumerated(value = EnumType.STRING)
@Column(name = "user_type")
private UserType userType;
@PrePersist
public void prePersist() {
System.out.println("User#prePersist"); // called
}
}
@Embeddable
public class Audit {
@Temporal(value = TemporalType.TIMESTAMP)
@Column(name = "created_time")
private Date creationTime;
@Temporal(value = TemporalType.TIMESTAMP)
@Column(name = "updated_time")
private Date updatedTime;
@PrePersist
public void prePersist() {
System.out.println("Audit#prePersist"); // called
}
}
@Data
@Embeddable
public class ContactAddress {
@Column(name = "street")
private String streetAddress;
@Column(name = "state")
private String state;
@Column(name = "city")
private String city;
@Column(name = "zip_code")
private String areaCode;
@Column(name = "addr_type")
private String addressType;
@PrePersist
public void prePersist() {
System.out.println("ContactAddress#prePersist"); // NOT called
}
}
And in the interaction below the @PrePersist
on ContactAddress
is not called. It is called on Audit
though.
txRunner.runInNewTransaction(() -> {
User user = new User();
user.setId(1);
user.setUserName("J Doe");
ContactAddress address = new ContactAddress();
address.setCity("Las Vegas");
address.setState("NV");
user.setAddress(Collections.singletonList(address));
em.persist(user);
});
Is this a bug? Expected? Has anyone else run into this?
Thank you!