当有 getter 时,Hibernate 是否总是需要 setter?
我们有一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如其他人所提到的,如果您注释属性 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).您应该使用 @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.如果您不想使用 setter,请设置
access="field"
:Set
access="field"
if you do not want to use setters:Hibernate 使用
set
方法来初始化您从数据库读取的实体。也许,如果你对实体字段进行访问修饰符
default
或protected
或public
那么 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
orprotected
orpublic
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.如果您不使用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.