JPA Entity Mapping for Your First Entity With Id and Column
Map your first JPA entity with Entity, Table, Id, GeneratedValue and Column, with the no argument constructor, equals and hashCode rules that Hibernate needs.

An entity is a class that Hibernate maps to a row in a table, and the first one you write decides habits that last for the life of the project. This guide builds a Customer entity on Hibernate 7 with Jakarta Persistence 3.2 and Spring Boot 4, controls its table and column names, lists the rules the class must follow and gets equals and hashCode right, which is the part most tutorials skip. The broader picture is in the Hibernate and JPA guide.
The smallest entity
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
protected Customer() { } // required by JPA
public Customer(String name) { this.name = name; }
public Long getId() { return id; }
public String getName() { return name; }
}With the default naming strategy this maps to a table customer with columns id and name. The class needs a constructor with no arguments, because Hibernate creates an empty instance and then sets the fields. It may be protected, which keeps application code on the real constructor. The @Id field is the primary key, and @GeneratedValue says the database produces it on insert. There is no setter for the id: nothing in the application should assign it.
Control the names and constraints
@Entity
@Table(name = "customers", uniqueConstraints = @UniqueConstraint(columnNames = "email"))
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "full_name", nullable = false, length = 120)
private String name;
@Column(nullable = false, length = 254)
private String email;
@Column(name = "created_at", nullable = false, updatable = false)
private Instant createdAt = Instant.now();
}@Column(nullable = false) and length do two things. Hibernate uses them if it generates the schema, and it checks nullable before it sends an insert, so a null name fails fast with a clear message instead of a database error. They do not replace constraints in the real schema, which belongs in migrations with a tool such as Flyway; a schema that exists only as annotations cannot be reviewed or versioned. updatable = false removes the column from update statements, a guard for values that should never change after creation. The email length of 254 matches the practical maximum of an address.
Rules the class must follow
An entity cannot be a record, and it should not be final. Hibernate sets fields after creating the object, and it may generate a subclass at run time for lazy proxies; a final class prevents that and silently turns lazy associations into eager loads. Records are excellent for projections and DTOs and a poor fit here.
Identity must be stable. Two references to the same row must compare as equal whether the object is new, saved or detached, and the next section shows how.
The no-argument constructor Hibernate calls must not depend on fields that are not yet set. Keep logic out of it, and out of the setters Hibernate uses, because field access bypasses them but property access does not. Choose field access, by placing @Id on the field as above, and keep it consistent for the whole class.
Equals and hashCode
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Customer other)) return false;
return id != null && id.equals(other.id);
}
@Override
public int hashCode() {
return getClass().hashCode();
}A constant hash with an id-based equals is the usual pattern for generated ids. The id is null before the first save, so a hash computed from it would change when the entity is persisted, and an entity stored in a HashSet before the save would then be lost inside it. The constant hash keeps every instance in one bucket, which is acceptable because entity sets are small. A new, unsaved entity equals only itself. If the entity has a natural business key such as an email, use that in both methods. The full reasoning, including proxies, is in equals and hashCode for generated ids, and the language contract is in the equals and hashCode contract. Avoid Lombok's @Data on entities. It generates equals, hashCode and toString over every field, including lazy collections, which is a quick route to a LazyInitializationException.
Check the SQL
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=trueStart the application, save a Customer and read the insert statement. With IDENTITY you will see an insert with no id and the generated key read back. Looking at the SQL while the model is small prevents surprises later, and it is a habit that debugging Hibernate SQL turns into a routine. Turn the logging off or move it to a development profile before you deploy.
Common first-entity errors
The most frequent failure is No default constructor for entity, which means the no-argument constructor is missing or private. The second is detached entity passed to persist, produced by calling persist on an object that already has an id, and the fix is merge or loading the entity first; see detached entities. The third is a column that is too short: Data truncation: Data too long for column appears when a string exceeds length, so size columns from the data and validate input with @Size before it reaches the database.
A fourth shows up at deploy time. With ddl-auto=update the application changes the schema itself, which hides mapping errors in development and is dangerous elsewhere. Set it to validate in every shared environment so a mismatch between entity and table fails at startup, and let migrations own the schema.
A quick test to keep the mapping honest
@DataJpaTest
class CustomerMappingTest {
@Autowired TestEntityManager em;
@Test
void persistsAndReloadsACustomer() {
Long id = em.persistAndGetId(new Customer("Ada"), Long.class);
em.flush();
em.clear();
assertThat(em.find(Customer.class, id).getName()).isEqualTo("Ada");
}
}The flush writes the insert and clear empties the persistence context, so find really reads the row back. Without clear the test would read the cached object and prove nothing about the mapping.
Naming strategy and the Spring Boot defaults
Spring Boot swaps Hibernate's default naming for its own strategy, which turns createdAt into created_at and Customer into customer. That is why the explicit @Column(name = "created_at") above is redundant under Boot and necessary in a plain Hibernate project. Pick one approach and keep it: either rely on the strategy everywhere or name everything explicitly, because a mix makes the mapping depend on a setting that few people remember.
Entity checklist before the first commit
Check five things before the first entity is committed. It has a no-argument constructor that is at least protected. It is not final, and none of its methods are. The id is generated and has no public setter. Equality uses the id and a constant hash code, or a natural key. Column lengths and nullability match the migration. Writing these down as a short review checklist saves repeating them in every pull request that adds an entity.
What to do next
The choice of GenerationType affects batching and portability, and IDENTITY disables JDBC insert batching, so it deserves a decision before the table is large; see id generation strategies. Relationships between entities come next, in OneToMany and ManyToOne. The specification is Jakarta Persistence, and the Hibernate ORM user guide covers entities in depth.


