我有一种感觉,向实体添加标有 @Transient 注释的字段非常容易出现错误。我说得对吗?

发布于 2024-09-03 11:25:44 字数 977 浏览 1 评论 0原文

我有一些哲学直观的感觉,添加未映射到数据库的字段会破坏实体类,并且是解决问题的错误方法。

但是,是否存在使用 @Transient 字段导致隐式且难以修复的问题的具体情况?

例如,当我们的实体中存在 @Transient 字段时,添加或删除二级缓存是否可能会破坏我们的应用程序?

相当大的更新:在对 @Transient 字段进行一些思考之后,在我看来,@Transient 字段应该以正确的方式使用。

我所说的“正确方式”是指实体始终应该具有相同的行为。这意味着当 getter 根据 @Transient 字段值不时返回 null 时,这是一种非常容易出错的行为。这意味着 @Transient 字段应该始终被初始化。

我只看到两种正确使用的情况:

  1. @Transient 字段应该在对象的构造函数中初始化:

    <前><代码>@Entity 公共类 SomeEntity @ID 私人长ID; @瞬态 私有字符串瞬态字段; 公共 SomeEntity () { 瞬态字段=“一些字符串”; } ... }
  2. @Transient 字段可以延迟初始化:

    <前><代码>@Entity 公共类 SomeEntity @ID 私人长ID; @瞬态 私有字符串瞬态字段; 公共字符串 getTransientField () { 同步(锁){ if (transientField == null) { 瞬态字段=“一些字符串”; } } 返回瞬态字段; } ... }

任何人都可以评论这两种情况或描述我错过的其他情况吗?

I have some philosophical intuitive feeling that adding fields which doesn't mapped to the DB corrupts entity classes and is a wrong way of solving problems.

But are there any concrete situations where using @Transient fields leads to implicit and hard fixing problems?

For example, is it possible that adding or removing 2nd level cache will break our app when there are @Transient fields in our entities?

Considerable update: after some thinking on @Transient fields it seems to me that @Transient fields just should be used in a proper way.

By 'proper way' I mean that entity always should have same behavior. It means that it's a very error-prone behavior when getters returns null's from time to time depending on @Transient field value. And it means that @Transient fields should always be initialized.

And I see only 2 cases of proper usage:

  1. @Transient fields should be initialized in object's constructor:

    @Entity
    public class SomeEntity
       @Id
       private long id;
    
       @Transient
       private String transientField;
    
       public SomeEntity () {
          transientField = "some string";
       }       
       ...
    }
    
  2. @Transient fields can be lazy initialized:

    @Entity
    public class SomeEntity
       @Id
       private long id;
    
       @Transient
       private String transientField;
    
       public String getTransientField () {
          synchronized (lock) {
             if (transientField == null) {
                transientField = "some string";
             }   
          }
          return transientField;
       }       
       ...
    }
    

Can anyone coment these 2 cases or describe other cases which I missed?

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

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

发布评论

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

评论(1

七色彩虹 2024-09-10 11:25:44

我在一些项目中使用了 Transient 注释,这些项目也持续使用 hibernate,并且还没有任何问题。

它通常用于可以由其他持久属性确定的字段,并且使用缓存也应该起作用,因为 Java 序列化机制(缓存通常期望缓存的对象是可序列化的)也考虑了 Transient 注释。我认为最好尽可能使用提供信息的瞬态 getter 和 setter 属性,而不是实例字段。

I am using the Transient annotation in some projects that persist with hibernate as well and didn't have any problems yet.

It is usually used for fields that can be determined by other persistent properties and using a cache should work also, because Javas Serialization mechanisms (caches usually expect the cached objects to be serializable) take the Transient annotation into consideration, too. I think it is preferrable to use transient getter and setter properties that provide the information instead of instance fields whenever possible.

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