Converts JSON into an @Entity class for Spring Data JPA on Spring Boot 3. The single Java file includes jakarta.persistence and jakarta.validation imports, an @Id primary key, a private field per key, Lombok's @Getter, @Setter and @FieldNameConstants, and Bean Validation's @NotEmpty/@NotNull.
It is aimed at Spring Boot backend work where you need an entity mapped to a table quickly, starting from sample JSON from an integration partner or a screen spec.
Model name (default MyModel). Its lowercase form is also used at the end of the package (com.hello.world.reservation).package line to your project's package, save it as Reservation.java, and create a repository extending JpaRepository<Reservation, Long>.{
"guest_name": "Hana Lee",
"room_no": 1204,
"nights": 3,
"rate": 149.5,
"breakfast": true,
"check_in": "2024-08-09T15:00:00"
}package com.hello.world.reservation;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.time.LocalDateTime;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.FieldNameConstants;
@Entity
@FieldNameConstants
@Getter
@Setter
public class Reservation {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotEmpty()
@Column(name = "guest_name")
private String guestName;
@NotNull()
@Column(name = "room_no")
private Long roomNo;
@NotNull()
private Long nights;
@NotNull()
private Double rate;
@NotNull()
private Boolean breakfast;
@NotNull()
@Column(name = "check_in")
private LocalDateTime checkIn;
}Integers like room_no and nights became Long, rate Double, and check_in LocalDateTime. Each snake_case key gets @Column(name = "..."), and since every value is filled in, the String field guestName gets @NotEmpty() and the others @NotNull().
Long, decimals → Double, booleans → Boolean, YYYY-MM-DD → LocalDate, date-times → LocalDateTime, other strings (digit-only ones included) → String.Map<String, Object> and arrays List<...>, both annotated @JdbcTypeCode(SqlTypes.JSON) so Hibernate 6 stores them in a JSON column.@Column(name = "guest_name") keeps the original key as the column name whenever the Java field name differs; keys that are Java keywords, like class, become class_.id or an integer id gives @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id, a string id gives a plain @Id private String id.@NotEmpty() when they are non-empty strings and @NotNull() otherwise, since @NotEmpty only applies to strings and collections.@FieldNameConstants generates name constants such as Reservation.Fields.guestName.When reservations or orders from another system must be pulled periodically and saved, one response JSON gives you the entity, and the batch job skeleton around saveAll() follows quickly.
With spring.jpa.hibernate.ddl-auto=update enabled locally, Hibernate creates the table from the generated entity, so you can load data for UI work right away.
jakarta. back to javax. and replace @JdbcTypeCode with a converter, since it does not exist in Hibernate 5.149.0 in JSON parses to the same number as 149, so it becomes Long. A decimal field needs a fractional value in the sample to come out as Double, and money is better served by BigDecimal.2024-08-09T15:00:00+09:00, lose the offset in LocalDateTime. Use OffsetDateTime or Instant when the instant matters.Map with an @Embedded class or an @OneToMany association.@Column(name = ...) on fields Spring would map to snake_case anyway?Spring Boot's default naming strategy happens to turn guestName into guest_name, but that breaks as soon as a key is not plain snake_case or the strategy is changed. The explicit name keeps the mapping correct regardless of configuration.
@NotNull enforced when saving?With spring-boot-starter-validation (Hibernate Validator) on the classpath, Hibernate runs Bean Validation right before persist and update and throws ConstraintViolationException. Without it, the annotations are ignored.
@FieldNameConstants for?Lombok generates string constants such as Reservation.Fields.checkIn. Use them in Sort.by(Reservation.Fields.checkIn) or in Specification/Criteria queries instead of string literals, so renaming a field breaks the build rather than the query.