JPA实体上的空构造方法和设置方法 [英] Empty constructors and setters on JPA Entites

查看:159
本文介绍了JPA实体上的空构造方法和设置方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不喜欢在JPA实体上至少有一个空的构造函数和公共设置器的要求.虽然我从EntityManager方面了解了问题,但这使类不变式失效.

I don't like the requirement on have at least one empty constructor and public setters on JPA entities. While I understand the issue on the EntityManager side, this invalidates class invariants.

有人对此解决方案(设计模式或惯用语言级别)有解决方案吗?

Does anyone have a solution for this (design pattern or idiom level) ?

谢谢!

伊戈尔

推荐答案

对于JPA,默认构造函数是必需的,但是,不需要使用setter.您可以根据放置批注的位置选择属性访问策略(字段或方法).

With JPA, the default constructor is required, however, you are not required to use setters. You can choose a property access strategy(field or method) based on where you place the annotations.

以下代码将使用直接字段访问,并且将成为不带setter的实体的一部分:

The following code will use direct field access and will work as a part of an entity without a setter:

@Column(name = DESCRIPTION)
private String description;

public String getDescription() { return description; }

通过设置器访问方法:

private String description;

@Column(name = DESCRIPTION)
public void setDescription(String description) {
     this.description = description;
}

public String getDescription() { return description; }

这篇关于JPA实体上的空构造方法和设置方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆