Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

What is wrong with this entity?

Java
@Entity
public final class Product {
@Id
private Long id;
private String name;
private Status status;
public Product(String name) {
this.name = name;
}
}

The entity has four problems. It has no no-argument constructor, which JPA requires (public or protected); it is final, which stops Hibernate from creating lazy proxies; its @Id has no @GeneratedValue, so the application must assign every id itself; and its enum status is stored as ORDINAL by default, so reordering the constants silently corrupts data. Putting @Id on the field also means Hibernate reads and writes fields directly, which is fine but worth knowing.

Why this question is asked

Small entity classes like this one look harmless, and most of their problems do not show up in the first test. One fails at the first load, one hides until a lazy association needs a proxy, and one waits until someone reorders an enum a year later. Interviewers use code like this to check that we know what JPA needs from an entity class and why. In this answer, we will go through each problem in the order Hibernate would hit it, show what breaks, and finish with the corrected entity.

1What JPA Needs From an Entity Class

An entity is a Java class whose instances Hibernate saves to and loads from a table. Hibernate creates entity objects itself when it loads rows, and sometimes creates subclasses of them at runtime. That gives it a few requirements: a no-argument constructor it can call, a class it can extend, a way to know or generate the id, and a clear rule for converting each field to a column. Our Product breaks most of them.

2Problem 1: No No-Argument Constructor

When Hibernate loads a row, it first creates an empty Product and then sets its fields from the columns. To create the empty object it calls the no-argument constructor. The Jakarta Persistence specification requires one, public or protected. Our class has only Product(String name), and because we declared a constructor, Java no longer generates the default one.

Java
@Entity public final class Product {
@Id private Long id;
private String name;
private Status status;
public Product(String name) { this.name = name; }
}
productRepository.findById(1L);
// org.hibernate.InstantiationException: No default constructor for entity: Product
// (exact wording varies by Hibernate version)

The fix is a protected no-argument constructor. Protected keeps it out of ordinary application code, so the public API still requires a name, while Hibernate can use it.

3Problem 2: A Final Class Cannot Be Proxied

For lazy loading, Hibernate often needs a proxy: an object that stands in for a Product that has not been loaded yet. If an OrderLine has @ManyToOne(fetch = LAZY) Product product, Hibernate gives the order line a proxy, and only runs the SELECT when we touch the product. The same happens with getReferenceById(). Hibernate creates that proxy by generating a subclass of Product at runtime, with ByteBuddy.

A final class cannot be subclassed, so Hibernate cannot create the proxy. Depending on the version and the setting, it logs a warning and falls back to loading the entity eagerly, or it fails. Either way, lazy loading for Product no longer works, and a query that loads 500 order lines may now also load 500 products. Final methods have the same effect on a smaller scale, since the proxy cannot intercept them. So entities should be neither final nor have final public methods.

Step throughWhy the proxy needs a subclass
OrderLine queryHibernateProduct proxyDatabase
load order line with lazy product
create subclass instance with only the id
line.getProduct().getName()
SELECT product by id, on first use
row

5 messages in order. Play them, or step through them one at a time.

A final class has no subclass, so there is no proxy and no lazy loading.

4Problem 3: No @GeneratedValue

@Id alone means the application is responsible for the id. Hibernate will not invent one. Our constructor never sets id, so persisting a new Product fails.

Java
entityManager.persist(new Product("Lamp"));
// IdentifierGenerationException: ids for this class must be manually assigned before calling save()
// (Hibernate's message; wording varies by version)
// Fix, when the database should generate ids:
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE) // or IDENTITY for auto-increment columns
private Long id;

Sometimes manual ids are intended, for example natural keys or UUIDs created in the domain. If so, the constructor must set it. Otherwise we add a generation strategy. With Spring Data, a manually assigned id has a second effect: save() sees a non-null id and assumes the entity already exists, so it calls merge, which runs a SELECT before the INSERT. Implementing Persistable or using a @Version field avoids that.

5Problem 4: The Enum Is Stored as a Number

Without @Enumerated, JPA stores an enum by its ordinal: its position in the declaration, starting at 0. That works until the declaration changes.

Java
enum Status { DRAFT, ACTIVE, RETIRED } // DRAFT=0, ACTIVE=1, RETIRED=2
// product saved as ACTIVE -> status column = 1
enum Status { DRAFT, REVIEW, ACTIVE, RETIRED } // someone adds REVIEW in the middle
// the same row still holds 1, which now reads back as REVIEW

Nothing fails. Every active product silently becomes a product under review, and the data is wrong in a way no exception reports. The fix is @Enumerated(EnumType.STRING), which stores the name, such as 'ACTIVE'. Adding or reordering constants is then safe; renaming one still needs a migration, which is at least visible.

6Field Access and the Corrected Entity

Where we put @Id decides the access type for the whole entity. On a field, Hibernate uses field access: it reads and writes the fields directly through reflection and ignores getters and setters. That is the common choice, because it lets us keep setters out of the public API and put logic in getters without affecting persistence. If @Id were on a getter, Hibernate would use property access and call getters and setters instead. Mixing the two by accident leads to annotations that are silently ignored.

Java
@Entity
public class Product { // not final
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String name;
@Enumerated(EnumType.STRING)
private Status status = Status.DRAFT;
protected Product() {} // for Hibernate
public Product(String name) { this.name = name; }
// getters; equals and hashCode based on id, handled carefully for new entities
}
The four problems and their fixes
ProblemWhat breaksFix
No no-arg constructorLoading fails with an instantiation errorprotected Product() {}
final classNo lazy proxies, extra eager loadsRemove final
@Id without generationpersist fails unless id is set@GeneratedValue or assign in the constructor
Enum as ORDINALReordering corrupts data silently@Enumerated(EnumType.STRING)

7Follow-up questions

Does Kotlin or Lombok change any of this?

The requirements are the same, but the tools help. In Kotlin, classes are final by default, so the kotlin-jpa and all-open compiler plugins generate no-argument constructors and open the entity classes. With Lombok, @NoArgsConstructor(access = AccessLevel.PROTECTED) adds the constructor; @Data should be avoided on entities because its equals, hashCode and toString can trigger lazy loading.

Can an entity be a Java record?

No. Records are final and have only final fields and a canonical constructor, which conflicts with proxies, the no-argument constructor and dirty checking. Records work very well as embeddables in recent Hibernate versions and as DTO projections in queries, but not as entities.

Is ORDINAL ever the right choice?

Rarely. It saves a few bytes per row, but makes the data unreadable and fragile. If storage size matters, an AttributeConverter that stores a short stable code per constant gives compact storage without depending on declaration order.

8Conclusion

In this answer, we found four problems in a small entity. JPA needs a public or protected no-argument constructor to create objects when loading rows, Hibernate needs a non-final class to generate lazy proxies, an @Id without @GeneratedValue must be assigned by the application, and an enum without @Enumerated(EnumType.STRING) is stored by position, so reordering the constants corrupts data silently. We also saw that @Id on a field selects field access, so Hibernate works with fields directly rather than getters and setters.

What to say in the interviewAbout 80 seconds aloud

Open with

I see four problems. First, there is no no-argument constructor. Hibernate creates an empty instance when it loads a row and then fills the fields, and the JPA specification requires a public or protected no-arg constructor.

Then, if they want more

Because we declared Product(String name), Java does not generate one, so loading fails. I would add a protected one. Second, the class is final. Hibernate makes lazy proxies by generating a subclass at runtime, for lazy many-to-one associations and getReference. With a final class it cannot, so lazy loading of products stops working or Hibernate refuses, depending on the version.

Third, @Id has no @GeneratedValue, so the application must set the id; the constructor never does, so persist fails. Either add a generation strategy like SEQUENCE, or assign a natural or UUID id in the constructor, knowing that Spring Data's save will then call merge for it.

Fourth, status is an enum with no @Enumerated, so it is stored as its ordinal. If someone inserts a constant in the middle, every stored number now means a different status, with no error at all. @Enumerated EnumType STRING fixes that.

Finally, @Id on the field means field access, so Hibernate uses the fields directly and ignores getters and setters, which is usually what we want.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is wrong with this entity?": https://scrivio-interview-library.netlify.app/databases/jpa-fundamentals/0318ab2673/