Reputation: 41919
In my Person
entity, let's say I'd like to make an immutable field - name
.
@Entity
public class Person {
@Id
private Long id;
@Column(name = "name")
private String name;
public Person() {} // no-args constructor for Hibernate
// getter for id and name fields
}
As I understand, the following is good enough for Hibernate to retrieve a person based on an id
look-up - it'll use Hibernate's setters
for the id
and name
.
But, if I want to create a new Person
with an immutable name
field, how would I need to change the above code?
Upvotes: 0
Views: 109
Reputation: 3707
Add a constructor that takes a name arg. The no-arg constructor needs to exist for Hibernate, but it doesn't need to be public. Force calling code to use the constructor with name arg.
Or use a static Builder class which would be able to directly set private field; but that seems like overkill in this case.
Upvotes: 2