当有 getter 时,Hibernate 是否总是需要 setter?

发布于 2024-08-29 09:48:15 字数 208 浏览 10 评论 0原文

我们有一些 Hibernate getter 方法,同时使用 @Column@Basic 进行注释。

如果我们没有相应的 setter,就会出现异常。这是为什么呢?

在我们的例子中,我们导出从 getter 返回的值(以存储在数据库中),而 setter 没有任何功能用途。所以我们只有一个空方法来解决错误情况。

We have some Hibernate getter methods annotated with both @Column and @Basic.

We get an exception if we don't have the corresponding setter. Why is this?

In our case we are deriving the value returned from the getter (to get stored in the DB) and the setter has no functional purpose. So we just have an empty method to get around the error condition..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

意中人 2024-09-05 09:48:15

正如其他人所提到的,如果您注释属性 getter 方法,那么 Hibernate 在从数据库读取值时将使用 setter。基本上,Hibernate 假设它写入数据库的任何内容最终都需要从数据库中读取。这意味着如果您注释 getter,那么它在从数据库读取对象时需要调用 setter。

您可以将 setter 设为私有(Hibernate 将使用反射来访问 setter)。这是保留类契约的好方法,同时仍然使用 Hibernate 进行关系映射。

如果该字段是从类中的其他属性派生的,那么为什么要将其存储在数据库中?您可以使用 @Transient 注释来标记该字段不应存储在数据库中。您甚至可以使用 @Formula< /code>注释让 Hibernate 为您派生字段(它通过使用发送到数据库的查询中的公式来实现这一点)。

As others have mentioned, if you annotate a property getter method, then Hibernate uses the setter when reading values from the database. Basically, Hibernate assumes that anything that it is writing to the database will eventually need to be read from the database. This implies that if you annotate a getter, then it needs to call a setter when reading the object from the database.

You can make the setter private (Hibernate will use reflection to access the setter). This is great way to preserve the contract of your class while still using Hibernate for relational mapping.

If the field is derived from other properties in the class, then why are you storing it in the database? You can use the @Transient annotation to mark the field that it shouldn't be stored in the database. You can even use the @Formula annotation to have Hibernate derive the field for you (it does this by using the formula in the query it sends to the database).

与风相奔跑 2024-09-05 09:48:15

您应该使用 @Entity(access = AccessType.FIELD) 注释您的类并注释您的属性。这应该可以解决你的问题。 setter 是支持重构的最佳方式。那里有小二传手有什么问题呢?

You should annotate your classes with the @Entity(access = AccessType.FIELD) and annotate your attributes. This should solve your problem. The setter is the best way to support refactoring. And what's the problem with having the little setter there.

李不 2024-09-05 09:48:15

如果您不想使用 setter,请设置 access="field"

<class name="com.demo.hibernate.Country" table="country">
  <id name="countryId" column="id" type="int">
    <generator class="increment" />
  </id>
  <property name="name" column="name" access="field" type="string" />
  <property name="countryCode" column="country_code" access="field" type="string" />
</class>

Set access="field" if you do not want to use setters:

<class name="com.demo.hibernate.Country" table="country">
  <id name="countryId" column="id" type="int">
    <generator class="increment" />
  </id>
  <property name="name" column="name" access="field" type="string" />
  <property name="countryCode" column="country_code" access="field" type="string" />
</class>
故事还在继续 2024-09-05 09:48:15

Hibernate 使用 set 方法来初始化您从数据库读取的实体。

也许,如果你对实体字段进行访问修饰符 defaultprotectedpublic 那么 Hibernate 将直接初始化字段而不使用 setter (我读过一些关于但我不确定它是否有效)。但使用 setter 是更优选的方式。

Hibernate uses set method to initialize the entity which you're reading from your DB.

Maybe, if you make access modifiers of entity fields default or protected or public then Hibernate will initialize fields directly without using setter (I read something about it but I'm not sure that it works). But using setter is much more preferred way.

独木成林 2024-09-05 09:48:15

如果您不使用setter并使用私有属性,Hibernate将必须通过反射检索Fields并执行field.setAccessible(true)。我不认为 Hibernate 会这样做。

我真的不知道我们是否可以告诉 Hibernate 这样做,但据我记得,默认配置是使用 setter...将 log/sysout 放在集合上,您会看到它使用 setter。

If you don't use setters and use private attributes, Hibernate would have to retrieve Fields by reflection and do field.setAccessible(true). I don't think Hibernate does that.

I don't really know if we can tell to Hibernate to do that, but as far as i remember, default config is using setters... Put a log/sysout on a set and you'll see that it uses the setter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文