简体   繁体   English

JPA,XML-是否普遍接受无参数的私有构造函数?

[英]JPA, XML - Is a no-arg private constructor generally accepted?

There are many situations where having a private no-arg constructor makes sense, for instance: 在许多情况下,使用私有的无参数构造函数很有意义,例如:

  • Instances that are designed to be immutable 设计为不可变的实例
  • Value Objects that are built with a builder, using a fluent API 使用流畅的API值由构建器构建的对象

When I'm willing to use these objects in a persistence unit or / and as transferable objects in a REST interface, I add some JPA or / and XML annotations. 当我愿意在持久性单元或/中使用这些对象,并愿意在REST接口中作为可转移对象使用时,我会添加一些JPA或/和XML批注。 In this process, I want my immutable objects to remain immutable objects and I want my Value Objects to remain Value Objects. 在此过程中,我希望我的不可变对象保持不变,而我的价值对象仍保持价值对象。 After all, why should I change the nature of these objects just because I want to use them as transferable objects or as persistent data? 毕竟,为什么仅因为我想将它们用作可传输对象或用作持久数据而更改这些对象的性质?

Example: 例:

@XmlType
public class Endpoint {
    @XmlElement
    private String hostname = "localhost";

    @XmlElement
    private int port = 8080;

    @XmlElement
    private String path = "/";

    private Endpoint() {}

    public String hostname() {
        return hostname;
    }

    // etc...

    @XmlTransient
    static public class Builder {
        private Endpoint endpoint = new Endpoint();

        public Builder hostname(String hostname) {
            endpoint.hostname = hostname;
            return this;
        }

        // etc...

        public Endpoint build() {
            Endpoint newInstance = endpoint;
            endpoint = null;
            return newInstance;
        }
    }
}

Of course, my favorite IDE starts reporting some warnings: there should be a public no-arg constructor,... I could change my no-arg constructor and make it public, and therefore allow my clients to use it. 当然,我最喜欢的IDE开始报告一些警告:应该有一个公共的无参数构造函数,...我可以更改无参数的构造函数并将其公开,因此允许我的客户使用它。 But it breaks the initial idea to always use the builder to construct instances of the object. 但这打破了最初的想法,始终使用构建器来构造对象的实例。 And anyway, there are situations where using the default constructor wouldn't make any sense. 无论如何,在某些情况下使用默认构造函数没有任何意义。

As far as I can remember, all the JAXB or JPA implementations I've played with have been able to instantiate my objects. 据我所记得,我玩过的所有JAXB或JPA实现都能够实例化我的对象。 I've tried JAXB RI, MOXy, Hybernate, EclipseLink; 我已经尝试过JAXB RI,MOXy,Hybernate,EclipseLink; all these libraries seems to accept my constructs without any problem. 所有这些库似乎都可以毫无问题地接受我的构造。

Hence my question: Is the requirement to have a public no-arg constructor (and not protected or private) somehow obsolete (Java 8+) with recent libraries? 因此,我的问题是:是否要求在最近的库中以某种方式淘汰(Java 8+)过时的公共无参数构造函数(而不是受保护的或私有的)? Or am I taking risks and I'm just lucky? 还是我冒险,我只是幸运?

JPA spec 2.1 "The Entity Class" JPA规范2.1“实体类”

The entity class must have a no-arg constructor. 实体类必须具有no-arg构造函数。 The entity class may have other constructors as well. 实体类也可以具有其他构造函数。 The no-arg constructor must be public or protected . no-arg构造函数必须是publicprotected

Some JPA providers (eg DataNucleus) don't require such a constructor at all (since they have features that add it during bytecode enhancement), but to be totally compliant (and not leaving things to being "lucky") the user should provide one. 一些JPA提供程序(例如DataNucleus)根本不需要这样的构造函数(因为它们具有在字节码增强过程中添加它的功能),但是为了完全兼容(并且不要让事情变得“幸运”),用户应该提供一个构造函数。 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM