21

Why does JPA require a no-arg constructor for domain objects? I am using eclipselink and just got this exception during deployment.

Exception [EclipseLink-63] (Eclipse Persistence Services-1.1.0.r3639-SNAPSHOT): 
org.eclipse.persistence.exceptions.DescriptorException

Exception Description: The instance creation method   
[com.me.model.UserVO.<Default Constructor>], with no parameters, 
  does not exist, or is not accessible.
Internal Exception: java.lang.NoSuchMethodException: 
  com.me.model.UserVO.<init>()
Descriptor: RelationalDescriptor(com.me.model.UserVO --> 
  [DatabaseTable(user)])
Michu93
  • 5,058
  • 7
  • 47
  • 80
Jacques René Mesrine
  • 46,127
  • 27
  • 66
  • 104

2 Answers2

24

Because it often happens that the JPA provider has to instantiate your domain object dynamically. It cannot do so, unless there is a no-arg constructor - it can't guess what the arguments should be.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 1
    Does that also mean that the no-arg constructor might get called after persisting an entity (e.g. when the entity manager initially loads an entity). In this case, we should not initialize any fields or do some other work in the constructor, right? – Theo Apr 28 '11 at 13:49
  • @Theo - yes, the constructor is called when you load an entity. – Bozho Apr 28 '11 at 13:55
14

Also notice that this is not provider dependent. It is a JPA specification.

JPA v2.0 JSR-317 and v2.1 JSR-338 says:

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.

José Andias
  • 1,894
  • 2
  • 29
  • 31