在我的实体对象中,我将所有 JPA 注释放在字段上。它们与 Project Lombok 配合得很好,它为我生成了所有的 getter 和 setter,极大地简化了我的代码。
编辑:
当我将注释向下移动到方法时,这似乎也会发生
@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
但是,我的 OneToMany 关联字段之一的自定义 getter 似乎未被调用。甚至像这样具有破坏性的东西
public Set<ChannelDAO> getChannels() {
throw new RuntimeException("Take that, Hibernate");
}
getChannels().add(new Channel("someChannel"));
永远不会抛出该异常。
这是怎么回事?为什么我的 getter 没有被调用?
最佳答案
Hibernate 默认使用反射。方法仅适用于您自己的 API。这减少了设置持久层所需的脚手架。
关于java - 自定义方法永远不会被调用,即使方法上有注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6700985/