java - 确保 JPA 使用 setter

标签 java hibernate jpa orm entity

我正在为我的实体类使用 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/

相关文章:

java - 替换Java中的正则表达式匹配

java - Jersey2.0及以上版本如何创建RESTful web服务客户端

java - 如何使用简单的 hibernate 查询更新 blob 字段

java - Android 游戏中不允许使用 W+E 加载段

java - 在另一个时区格式化本地日期

java - hibernate 和斯卡拉

java - 在 Java 中使用 Hibernate 插入 70000 条记录时出现网关超时错误

java - @OneToOne 实体关系的 JPA 合并

java - JPA 多对多关系 "JoinColumn cannot be resolved to a type"错误

java - 当前最佳的单元测试 EJB3/JPA 框架