JSR 338: JavaTM Persistence API 2.1 Specification > 2.1 The Entity Class specifies:
The entity class must not be final. No methods or persistent instance variables of the entity class may be final.
So I tried with Hibernate 5.2.18.Final
to prove it by setting an entity class as final
:
@EqualsAndHashCode
@Getter
@Setter
@Entity
public final class City {
@Id
@SequenceGenerator(name="city_sequence", sequenceName="city_seq", initialValue=0, allocationSize=25)
@GeneratedValue(strategy=SEQUENCE, generator="city_sequence")
private int id;
@Column(length=20, nullable=false)
private String city;
@ManyToOne(fetch = FetchType.LAZY)
private Province province;
}
However, the result is out of my expectation: the corresponding table can be generated and new instance of City
can be persisted to the database, meaning my code proves that entity class can be final
Question: Is there any better way to prove the entity class must not be final? Isn't it a hard-and-fast rule as it is in the specification?