我正在为我的实体类使用 JPA,但 JPA 不会为我的参数执行 setter 。我上了这门课:
@Entity
public class Groep implements Serializable, IGroep {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int groepID;
private String naam;
@Column(name = "MOTIVATIE", length = 1000)
private String motivatie;
protected Groep(){
}
@Override
public String getMotivatie() {
return motivatie;
}
public void setMotivatie(String motivatie) {
System.out.println("domein.Groep.setMotivatie()");
this.motivatie = motivatie;
}
}
但我从来没有看到 system.out... 有什么问题吗?
最佳答案
访问类型取决于您放置注释的位置。 您已将它们放在实例变量上,因此持久性提供程序将尝试通过反射直接访问它们。
如果您使用 hibernate 作为持久化提供者,您可以简单地以这种方式定义您的@Id:
As a JPA provider, Hibernate can introspect both the entity attributes (instance fields) or the accessors (instance properties). By default, the placement of the @Id annotation gives the default access strategy.
请记住,对于所有实体,您的注释放置策略应该保持一致。
关于java - 确保 JPA 使用 setter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42854345/