10

Is it necessary that the default contructor is empty when working with JPA? Since I have no clue of how JPA internally works I somehow fear that an object could be falsely initialised by JPA when the default contructor does some stuff on his own like filling attributes with default values and the like.

Thanks.

2

3 Answers 3

11

To minimize runtime overhead by JPA, but enforce constraints when creating new instances in user code:

  • use a protected no-arg constructor with no/little code to facilitate fastest-path materialization, and
  • an n-arg constructor for client code, containing code necessary to instantiate the new object per constraints.
0
7

The short answer is no. It is not required to be empty.

However, you need to be aware that when the JPA implementation materializes an instance from persistence, it will do things to the instance after the no-args constructor completes. This might undo things that your constructor has done.

To understand whether the constructor needs to be empty, you need to consider that the constructor is going to be called in two scenarios.

  • When you are creating a "new" object, the constructor needs to produce an object that is sufficiently initialized to be used by normal code. Fields that need to be initialized to non-default states should be dealt with.

  • When JPA is materializing an object from the persistent store, the materialization is then going to "fill in the details" of the newly constructed instance. This will typically overwrite the object state of the constructed object.

Of course, your code may be designed so that it doesn't directly use the no-args constructor. This renders the first scenario moot, and also renders moot the potentially wasteful "double initialization" that might occur.

4
  • and, by extension, extra (wasted) time to materialize an instance. Dec 9, 2012 at 0:35
  • 2
    @RichardSitze - that is a consideration. But generally speaking the time spent in unnecessary initialization is trivial compared with the time spent in materializing the object.
    – Stephen C
    Dec 9, 2012 at 0:44
  • "The short answer is no. It is not required to be empty." But the Java Persistence 2.1 specification specifically states: "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." Dec 19, 2020 at 22:25
  • None of that says that the no-args constructor must have an empty body. That's what this Q&A is about.
    – Stephen C
    Dec 20, 2020 at 1:38
5

The longer answer is: it shouldn't be empty, most of the time. For example, imagine the following entity:

@Entity
public class Team {
    @Id
    private Long id;

    @OneToMany
    private Set<Player> players;

    // getters
}

When an instance is loaded by JPA, the players set will never be null. It could be empty, but not null. But when you're creating a new Team, or when you're using Team instances in unit tests, this invariant should also be ensured. So the class should be written as

@Entity
public class Team {
    @Id
    private Long id;

    @OneToMany
    private Set<Player> players = new HashSet<Player>(0);

    // getters
}

which is equivalent to

@Entity
public class Team {
    @Id
    private Long id;

    @OneToMany
    private Set<Player> players;

    public Team() {
        this.players = new HashSet<Player>(0);
    }
    // getters
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.