In my Entity objects I put all JPA annotations on fields. They work well with Project Lombok which generates all getters and setters for me, greatly simplifying my code.
EDIT:
This seems to also be happening when I move the annotation down to the method
@OneToMany(targetEntity = ChannelDAOHb.class, mappedBy = "server", orphanRemoval = true)
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE})
public Set<ChannelDAO> getChannels() {
System.out.println("Getting some channels");
...
}
getChannels().add(new Channel("someChannel")); //Nothing printed here
However it seems the custom getter for one of my OneToMany association fields isn’t being called. Even something destructive like this
public Set<ChannelDAO> getChannels() {
throw new RuntimeException("Take that, Hibernate");
}
getChannels().add(new Channel("someChannel"));
That exception NEVER gets thrown.
What’s going on here? Why isn’t my getter being called?
Hibernate uses reflection by default. Methods are only for your own API. This reduces scaffolding needed for setting up persistent layer.