有没有办法为 Hibernate 管理的对象声明最终字段?

发布于 2024-07-21 14:37:34 字数 1161 浏览 8 评论 0原文

我刚刚开始使用 Hibernate,到目前为止我看到的所有示例看起来都非常像 Hibernate 文档中的教程:

package org.hibernate.tutorial.domain;
import java.util.Date;

public class Event {

    private Long id;
    private String title;
    private Date date;

    public Event() {}

    /* Accessor methods... */
}

具体来说:没有任何字段被声明为 final,并且有必须是无参构造函数,以便 Hibernate 框架可以实例化该类并设置其字段。

但事情是这样的 - 我真的不喜欢以任何方式让我的类可变,只要我能避免它(Java实践:不可变对象为这样做提供了一个非常有力的论据)。 那么,即使我将每个字段声明为“final”,有什么方法可以让 Hibernate 正常工作吗?

我知道 Hibernate 使用 Reflection 来实例化其类,因此需要能够调用某种构造函数,而不必冒选择错误构造函数或将错误值传递给其参数之一的风险,因此可能更安全调用无参数构造函数并一次设置每个字段。 但是,难道不应该向 Hibernate 提供必要的信息,以便它可以安全地实例化不可变对象吗?

public class Event {

    private final Long id;
    private final String title;
    private final Date date;

    public Event(@SetsProperty("id") Long id,
        @SetsProperty("title") String title,
        @SetsProperty("date") Date date) {

        this.id = id;
        this.title = title;
        this.date = new Date(date.getTime());
    }

    /* Accessor methods... */
}

@SetsProperty 注释当然是虚构的,但似乎并不应该遥不可及。

I'm just getting started with Hibernate, and all the examples I'm seeing so far look pretty much like the tutorial in the Hibernate documentation:

package org.hibernate.tutorial.domain;
import java.util.Date;

public class Event {

    private Long id;
    private String title;
    private Date date;

    public Event() {}

    /* Accessor methods... */
}

Specifically: none of the fields are declared as final, and there must be a no-argument constructor so that the Hibernate framework can instantiate the class and set its fields.

But here's the thing - I really don't like making my classes mutable in any way whenever I can avoid it (Java Practices: Immutable Objects make a pretty strong argument for doing this). So is there any way to get Hibernate to work even if I were to declare each of the fields 'final'?

I understand that Hibernate uses Reflection to instantiate its classes and therefore needs to be able to invoke a constructor of some sort without taking the risk that it would pick the wrong constructor or pass the wrong value to one of its parameters, so it's probably safer to invoke the no-arg constructor and set each field one at a time. However, shouldn't it be possible to provide the necessary information to Hibernate so that it can safely instantiate immutable objects?

public class Event {

    private final Long id;
    private final String title;
    private final Date date;

    public Event(@SetsProperty("id") Long id,
        @SetsProperty("title") String title,
        @SetsProperty("date") Date date) {

        this.id = id;
        this.title = title;
        this.date = new Date(date.getTime());
    }

    /* Accessor methods... */
}

The @SetsProperty annotation is of course fictitious, but doesn't seem like it should be out of reach.

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

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

发布评论

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

评论(6

甜点 2024-07-28 14:37:34

实际上,在 JDK 1.5+ 中,hibernate 可以处理(通过反射)更改最终字段。 创建一个受保护的默认构造函数(),将字段设置为某些默认值/null 等...Hibernate 可以并且将在实例化对象时覆盖这些值。

这一切都是可能的,这要归功于 Java 1.5 内存模型的更改——为了启用序列化/反序列化而进行的感兴趣的更改(允许最终不是那么最终)。

public class Event {

private final Long id;
private final String title;
private final Date date;

// Purely for serialization/deserialization
protected Event() {
    id = null;
    title = null;
    date = null;
}

public Event(Long id, String title, Data date) {
    this.id = id;
    this.title = title;
    this.date = date;
}

/* Accessor methods... */

}

Actually in JDK 1.5+ hibernate can handle (through reflection) changing final fields. Create a protected default constructor() that sets the fields to some defaults/null etc... Hibernate can and will override those values when it instantiates the object.

This is all possible thanks to changes to Java 1.5 memory model - the changes of interest (allowing final to be not so final) where made to enable serialization/deserialization.

public class Event {

private final Long id;
private final String title;
private final Date date;

// Purely for serialization/deserialization
protected Event() {
    id = null;
    title = null;
    date = null;
}

public Event(Long id, String title, Data date) {
    this.id = id;
    this.title = title;
    this.date = date;
}

/* Accessor methods... */

}

少年亿悲伤 2024-07-28 14:37:34

不可变对象是指没有修改其状态(即其字段)的方法的对象。 这些字段不必是最终的。 因此,您可以删除所有变元并将 Hibernate 配置为使用字段访问而不是访问器,或者您可以将无参数构造函数和变元标记为已弃用。 这是一个解决办法,但总比没有好。

Immutable object means an object with no methods that modify its state (i.e. its fields). The fields needn't be final. So you can remove all mutators and configure Hibernate to use fields acces instead of accessors or you can just mark no-arg constructor and mutators as deprecated. It's a bit workaround but better that than nothing.

旧竹 2024-07-28 14:37:34

这听起来好像不是 Hibernate 的用例,因为它执行的许多操作都涉及可变状态:

  • 合并对象
  • 脏状态检查
  • 刷新更改话

虽如此,如果您担心不变性,您可以选择为对象提供包装器,复制构造函数:

public class FinalEvent {
    private final Integer id;

    public FinalEvent(Event event) {
        id = event.id;
    }
}

但这确实意味着额外的工作。


现在我正在考虑这一点,休眠会话通常是线程绑定的,这至少使最终字段的好处之一失效 - 安全发布。

您还在寻找 Final 字段的其他哪些好处?

This sounds like it is not a use case for Hibernate, since many operations it performs concern mutable state:

  • merging objects
  • dirty state checking
  • flushing changes

That being said, if you're concerned about immutability you may choose to provide wrappers around your objects, with copy-constructors:

public class FinalEvent {
    private final Integer id;

    public FinalEvent(Event event) {
        id = event.id;
    }
}

It does mean extra work though.


Now that I'm thinking of it, hibernate sessions are usually thread-bound and this voids at least one the benefits of the final fields - safe publication.

What other the benefits of final fields are you looking for?

忘东忘西忘不掉你 2024-07-28 14:37:34

这个问题也困扰我很久了。 我最近一直在尝试的一个想法是:为模型类定义只读接口,并让 DAO 和任何工厂将这些接口返回到对象上。 这意味着即使实现是可变的,一旦它离开 DAO/工厂对象,就不能再对其进行调整。

就像这样:

public interface Grape {
  public Color getColor();
}

public class HibernateGrapeDao {
  public Grape findGrape(int grapeId) {
    HibernateGrape grape = hibernate.find(...
    return grape;
  }
}

class HibernateGrape implements Grape {
 ....
}

甚至可能希望将实现类保留为 dao 包的包私有,这样就没有人可以直接摆弄它们。 需要做更多的工作,但从长远来看可能有助于保持事情的清洁。 显然,要小心整个平等/身份事务。

This one has been bugging me for a long time too. One idea I've been trying out recently is this - define read-only interfaces for your model classes and have your DAOs and any factories return those instead on the object. That means that even though the implementation is mutable, once it's left the DAO/factory object it can no longer be tweaked.

Like so:

public interface Grape {
  public Color getColor();
}

public class HibernateGrapeDao {
  public Grape findGrape(int grapeId) {
    HibernateGrape grape = hibernate.find(...
    return grape;
  }
}

class HibernateGrape implements Grape {
 ....
}

May even want to keep the implementing classes package-private to the dao package, so nobody can fiddle with them directly. A bit more work, but probably helps keep things cleaner in the long run. And, obviously, be careful with the whole equality/identity business.

孤独患者 2024-07-28 14:37:34

您也许能够通过使用构建器模式来实现所需的结果。 我不久前读过一篇 帖子 Hibernate 论坛讨论了这个想法(尽管我自己从未实现过......)

You may be able to accomplish the desired result by using a Builder pattern. I read a posting a while ago on the Hibernate Forums discussing the idea (though I never implemented it myself...)

So尛奶瓶 2024-07-28 14:37:34

使用 @Access(AccessType.FIELD) 注释您的类,然后您可以使您的字段成为最终字段。 像这样:

@Access(AccessType.FIELD)
public final class Event {

    private final Long id;
    private final String title;
    private final Date date;

    private Event() {
        id = null;
        title = null;
        date = null;
    }

    public Event(Long id, String title, Date date) {
        this.id = id;
        this.title = title;
        this.date = date;
    }
}

Annotate your class with @Access(AccessType.FIELD) then you can make your fields final. Like this:

@Access(AccessType.FIELD)
public final class Event {

    private final Long id;
    private final String title;
    private final Date date;

    private Event() {
        id = null;
        title = null;
        date = null;
    }

    public Event(Long id, String title, Date date) {
        this.id = id;
        this.title = title;
        this.date = date;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文