I need some help with hibernate annotations.
I have the following 2 entities:
public class Custom {
private Map<KeyObject, ValueObject> properties;
@oneToMany(mappedBy = "customId", cascade = CascadeType.All)
@MapKey(name = "keyObject")
public Map<KeyObject, ValueObject> getProperties();
.....
}
public class ValueObject {
private KeyObject keyObject;
private Long customId;
private String value;
...getters and setters
}
I have a simple dao class to save, update, select, and delete records.
If I remove a record from the map in the Custom object and then call customDao.save(custom) my changes are never persisted to the database. It is not throwing any errors either.
I saw in the hibernate examples I can define the customId as a Custom object instead of a Long in the KeyValue table but I did not want to do this.
Am I setting up my entity’s incorrectly?
When an association has the
mappedByattribute, it means: I’m not the owner of this association. Any change done to this side of the association won’t matter for Hibernate. What will matter is the other side of the association.I’m surprised this even works, because I would have expected Hibernate to require a ManyToOne on the other side, of type
Custom. But what’s sure is that if you don’t set the custom/customId field to null in KeyValue/ValueObject, Hibernate won’t set this column to null.