Skip to content

Bethibande/jakarta-entity-dto-generator

Repository files navigation

Jakarta entity dto generator

This is a tool that generates DTO classes from your JPA entity classes

Features

  • Generate multiple DTO classes from a single Entity
  • Easily convert entities to DTOs
  • Generate virtual fields, see here for example
  • Generates NotNull annotations for fields if jakarta validation is used

Usage

Note

Please note that the library is built for Java 25.

Gradle

repositories {
    maven {
        url = uri("https://pckg.bethibande.com/repository/maven-releases/")
        name = "bethibande-releases"
    }
}

dependencies {
    implementation("com.bethibande.process:annotations:1.5")
    annotationProcessor("com.bethibande.process:processor:1.5")
}

Example

See here for the full example.

The following entity definition will generate two DTO classes: ExampleEntityDTO and ExampleEntityDTOWithoutId

@Entity
// This will generate a DTO of the full model
@EntityDTO(expandProperties = {"entity"})
// This will generate the same DTO but without id fields
@EntityDTO(excludeProperties = {"id", "entity.id"}, expandProperties = {"entity"})
public class ExampleEntity extends EntityBase {

    public String someString;

    @Embedded
    public EmbeddableTimestamp timestamp;

    @ManyToOne
    public ReferencedEntity entity;

}

You can easily convert your entities into a DTO

@GET
@Path("/api/v1/example-entity/{id}")
public ExampleEntityDTO findById(final @PathParam("id") long id) {
    final ExampleEntity entity = repository.findById(id);
    return ExampleEntityDTO.from(entity); // This is null-safe
}

The serialized output of this DTO might look like this:

{
  "someString": "abc",
  "created": "2025-11-22T20:51",
  "updated": "2025-11-22T20:51",
  "entity": {
    "name": "test",
    "id": 12
  },
  "id": 1
}

If we remove expandProperties = {"entity"} from the annotation the entity field will be deflated into a single entityId field like this:

{
  "someString": "abc",
  "created": "2025-11-22T20:51",
  "updated": "2025-11-22T20:51",
  "entityId": 12,
  "id": 1
}

About

Generate DTO classes for your JPA entities

Topics

Resources

License

Stars

Watchers

Forks

Languages