【问题标题】:Is there any difference between 'static transients' and 'transient Type aField' declaration for GORM?GORM 的“静态瞬态”和“瞬态类型 aField”声明之间有什么区别吗?
【发布时间】:2012-09-09 20:14:13
【问题描述】:

让我们考虑两个 Grails 域示例类。

一级:

class Person {

    String name
    Integer counter = 0

    static transients = ['counter']
}

二等:

class Vehicle {

    String name
    transient Integer counter = 0
}

PersonVehicle 类之间的 Integer counter 字段的 GORM 持久性或域类行为会有什么不同吗?

编辑:我知道 Person 类是 Grails 文档引用的好方法。但是我更喜欢 Vehicle 类方式,因为它似乎更明显,并且在阅读代码时更容易不被忽视。

【问题讨论】:

    标签: grails persistence grails-orm transient


    【解决方案1】:

    这两种机制定义了不同类型的“瞬态”。 static transients 定义不应被 Hibernate 映射到数据库的 bean 属性,而 transient 关键字表示不应由 Java 对象序列化机制保存的字段(例如,当使用 webflow 时)。它们在不同的情况下都有各自的用途。

    【讨论】:

    • 是的,这是真的。我有点意识到这一点,但是当一个属性被放入静态瞬态列表时,它也不会被序列化(例如,当使用“as XML”强制转换时)。所以它的行为类似于瞬态关键字。我现在也检查过,瞬态整数计数器无论如何都会导出到数据库模式,所以这两者之间确实没有共同点。不管怎样,你的回答很中肯,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2011-06-01
    • 2013-07-21
    • 2023-04-07
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多