简体   繁体   English

不需要no-arg构造函数的JPA实现

[英]JPA implementation that doesn't require no-arg constructor

Is there an ORM framework which implements JPA but doesn't require default (no-arg) constructor from @Entity classes? 是否有一个ORM框架实现JPA但不需要@Entity类的默认(no-arg)构造函数?

AFAIK, it is said in JPA documentation that an @Entity should have default constructor, but can an implementation support constructor arguments injection (or maybe unsafe object instantiation) and therefore work with classes without a default constructor? AFAIK,在JPA文档中说@Entity应该有默认构造函数,但实现是否可以支持构造函数参数注入(或者可能是不安全的对象实例化),因此可以使用没有默认构造函数的类?

This feature would help a lot to ensure immutability (which standard JPA lacks) and would let the developer keep strict invariants on class instance state (eg its fields always have the correct value). 此功能将有助于确保不可变性( 标准 JPA缺少)并且允许开发人员在类实例状态上保持严格的不变量(例如,其字段始终具有正确的值)。

By the way, when using Kotlin, the need of no-arg constructor interferes with good code style and coding conventions. 顺便说一下,当使用Kotlin时,对无参数构造函数的需求会干扰良好的代码风格和编码约定。

There's JIRM library which deals with constructor injection and immutability, but it's not a complete JPA implementation. JIRM库处理构造函数注入和不变性,但它不是一个完整的JPA实现。

So, does any of the JPA ORMs support @Entity classes with no default constructor? 那么,任何JPA ORM都支持没有默认构造函数的@Entity类吗?

Is there an ORM framework which implements JPA but doesn't require default (non-arg) constructor from @Entity classes? 是否有一个ORM框架实现JPA但不需要@Entity类的默认(非arg)构造函数?

No, because it would break the specification. 不,因为它会违反规范。 The below quote is from page 23 of the JPA 2.1 specification : 以下引用来自JPA 2.1规范的第23页:

The entity class must have a no-arg constructor. 实体类必须具有无参数构造函数。 The entity class may have other constructors as well. 实体类也可以有其他构造函数。 The no-arg constructor must be public or protected. no-arg构造函数必须是公共的或受保护的。

Having said that, there might exist some ORM framework that implements JPA partially that ignores this feature. 话虽如此,可能存在一些部分实现JPA的ORM框架忽略了这个功能。

是的,我尝试使用EclipseLink 2.5.2,即使没有提供,也可以正常工作。

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

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