Is there a way (e.g. an Eclipse plugin) for automatically creating a DTO from Entity (JPA)?

I need a simple DTO creation tool that will either

  • Create it on the fly (for example, cglib - create a class object and DTO on the fly)
  • Or an Eclipse plugin that takes Entity and generates a DTO (the user will indicate which tree graph will be included, and for not included, it will include foreign keys, not related objects, etc.).

eg. take something like this

@Entity
@Table(name="my_entity")
public class MyEntity {
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String name;

    @ManyToOne
    private RelatedEntity related;
     public RelatedEntity getRelated(){
          return related;
     }
     ...

And create something like this:

@Entity
@Table(name="my_entity")
public class MyEntity imlpements MyEntityDTO {
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String name;

    @ManyToOne
    private RelatedEntity related;
     //overrides MyEntity interface, it allowed to narrow return type 
     public RelatedEntity getRelated(){
          return related;
     }
     ...

     //implements MYEntityDTO respective interfaces

     public Long getRelatedId(){return related.getId();}

And the DTO interfaces:

public interface MyEntityDTO {

    public String getId();
    public String getName();
    public Long getRelatedId();
    public RelatedEntityDTO getRelated(); //RelatedEntity implements RelatedEntityDTO

    ...
}

public interface RelatedEntityDTO {
    ... 
}

If we do not want to include children in the schedule, remove it from the DTO interface:

public interface MyEntityDTO {

    public String getId();
    public String getName();
    public Long getRelatedId();

    ...

I am sure that there is an eclipse plugin for it, and if not, I force someone to write it or explain why what I want is not useful (and offer an alternative suggestion)

+3

All Articles