ricardo-bernardes/jpa-entity-generator icon
public
Published on 7/2/2025
JPA Entity Generator

Prompts
Generate JPA Entity
Create a JPA Entity class for Spring Boot applications
Based on the provided context, generate a JPA Entity adhering to these rules:

1. **Basic Structure**
- Class annotated with `@Entity`
- Class name: `{{EntityName}}` (PascalCase)
- Default package: `com.{{company}}.{{schema}}.model`
- Use Lombok: `@Data`, `@SuperBuilder`, `@EqualsAndHashCode`, `@AllArgsConstructor`
- Implement `Serializable`

2. **Table Mapping**
- `@Table(name = "{{table_name}}")` 
- Specify schema if not `public`: `@Table(schema = "{{schema}}")`
- Use `@Id` with `@GeneratedValue(strategy = GenerationType.IDENTITY)`

3. **Column Mapping**
- Annotate all fields with `@Column`
- Define `name`, `nullable`, and `length` per DDL
- Appropriate Java types:
  * `LocalDateTime` for timestamps
  * `Boolean` for flags
  * `BigDecimal` for monetary values
  * Enums: `@Enumerated(EnumType.STRING)`

4. **Soft Delete**
- If `deleted_at` or `is_deleted` exists:
  ```java
  @Column(name = "deleted_at")
  private LocalDateTime deletedAt;
  ```

5. **Relationships**
- `@ManyToOne` for FKs (default):
  ```java
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "foreign_key_column")
  private RelatedEntity related;
  ```
- Bidirectional `@OneToMany`:
  ```java
  @OneToMany(mappedBy = "parentField", cascade = CascadeType.ALL)
  private List<ChildEntity> children = new ArrayList<>();
  ```

6. **Auditing Rules**
- For `created_at`/`updated_at`:
  ```java
  @CreatedDate
  @Column(updatable = false)
  private LocalDateTime createdAt;
  
  @LastModifiedDate
  private LocalDateTime updatedAt;
  ```
- Add `@EntityListeners(AuditingEntityListener.class)` to the class

7. **Validations**
- Include constraints:
  * `@NotNull` for mandatory fields
  * `@Size(min=1, max=255)` for text
  * `@Positive` for numbers

8. **Best Practices**
- Use `FetchType.LAZY` for all relationships
- Initialize collections: `new ArrayList<>()`
- Implement `equals()`/`hashCode()` based on `id`

9. **Mandatory Header**
```java
/*
 * Copyright (c) {{YEAR}}, {{COMPANY}}. All rights reserved.
 * 
 * This source code is property of {{COMPANY}}. Unauthorized copying, modification, 
 * distribution or use is strictly prohibited.
 */
```

**Structure Example:**
```java
@Entity @Table(name = "customer", schema = "global") @NoArgsConstructor @Data @SuperBuilder       public class Customer implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "code", nullable = false, length = 10)
    @NotNull
    @Size(min = 5, max = 10)
    private String code;
    
    @Column(name = "deleted_at")
    private LocalDateTime deletedAt;
    
    @CreatedDate
    @Column(updatable = false)
    private LocalDateTime createdAt;
    
    @LastModifiedDate
    private LocalDateTime updatedAt;
    
    @OneToMany(mappedBy = "customer")
    private Set<Order> orders = new HashSet<>();
    
    // equals/hashCode using id
}
```

**Key Rules:**
- Mandatory fields: `nullable = false`
- Unique fields: `@Column(unique = true)`
- Numeric precision: `@Column(precision = 10, scale = 2)`
- Always use wrapper types (e.g., `Long`) over primitives