paste PostgreSQL or JSON, get your schema reviewed like a senior would

From CREATE TABLE to JPA Entity: the type mapping

Transcribing DDL into a JPA Entity is mechanical work full of small decisions: which Java type for NUMERIC? IDENTITY or SEQUENCE? Does nullable go in the annotation or do you trust the database? Here's the mapping Schemint uses and the reasoning behind each choice.

PostgreSQL to Java type table

PostgreSQLJavaNote
BIGSERIAL / BIGINTLonggenerated id: @GeneratedValue(strategy = IDENTITY)
SERIAL / INTEGERInteger
SMALLINTShort
VARCHAR(n) / TEXTString@Column(length = n) when there's a limit
NUMERIC(p,s)BigDecimalnever double for money
BOOLEANBoolean
TIMESTAMPLocalDateTimea wall-clock reading, no zone
TIMESTAMPTZInstant / OffsetDateTimea real instant, normalized to UTC
DATELocalDate
TIMELocalTime
UUIDUUID
BYTEAbyte[]
JSONBStringor @JdbcTypeCode(SqlTypes.JSON) with your own type

Wrappers, and the boolean trap

Wrapper types (Long, not long): a nullable column has to be able to represent absence, and a primitive with a magic 0 value is a bug waiting to happen. The sneakiest case is boolean: map a nullable BOOLEAN column to the primitive and Hibernate has to unbox null on read, which throws deep inside entity hydration, far from the line that caused it. The safe rule is mechanical: column is NOT NULL, primitive is allowed; column is nullable, wrapper is required. Schemint just uses wrappers everywhere so the generated class never depends on remembering the rule.

The generated skeleton

From the sample DDL (customers with name, unique email and audit columns), the full entity comes out as:

@Entity
@Table(name = "customers")
public class CustomerEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, length = 120)
    private String name;

    @Column(nullable = false, unique = true, length = 180)
    private String email;

    @Column(length = 20)
    private String document;

    @Column(nullable = false)
    private Boolean active = true;

    @Column(name = "created_at", nullable = false)
    private LocalDateTime createdAt;

    @Column(name = "updated_at")
    private LocalDateTime updatedAt;
}

What travels with the entity

The entity is rarely alone. The same column list yields the request record (everything except the id, generated fields and audit timestamps, because clients don't send those), the response record (everything), and the JpaRepository<CustomerEntity, Long>. Generating the four together keeps them agreeing on names and types, which is exactly the part that drifts when they're written by hand across three files on different days.

Paste your CREATE TABLE into Schemint and get back the Entity, request/response records, repository, Flyway migration, ER diagram and the schema warnings. It runs in the browser and nothing is stored.

Related