【问题标题】:Is there a way (e.g. an Eclipse plugin) to automatically generate a DTO from an Entity (JPA)?有没有办法(例如 Eclipse 插件)从实体(JPA)自动生成 DTO?
【发布时间】:2011-08-22 23:56:08
【问题描述】:

我想要一个简单的正向 DTO 生成工具,既可以

  1. 动态生成(例如 cglib - 动态创建类和 DTO 对象)
  2. 或一个 Eclipse 插件,它将获取实体并生成 DTO(用户将指定要包含的树形图,对于未包含的,将包含外键而不是相关实体等)

例如拿这样的东西

@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;
     }
     ...

然后生成这样的东西:

@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's allowed to narrow return type 
     public RelatedEntity getRelated(){
          return related;
     }
     ...

     //implements MYEntityDTO respective interfaces

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

和 DTO 接口:

public interface MyEntityDTO {

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

    ...
}

public interface RelatedEntityDTO {
    ... 
}

如果我们不想在图表中包含子项,请将其从 DTO 界面中删除:

public interface MyEntityDTO {

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

    ...

我确定有一些 eclipse 插件,如果没有,我会找人写一个,或者解释为什么我想要的东西没有帮助(并提供替代建议)

【问题讨论】:

标签: java eclipse jpa code-generation dto


【解决方案1】:

可能 Hibernate 工具应该这样做:http://hibernate.org/subprojects/tools.html

【讨论】:

  • 我找不到怎么做,你知道这是事实吗?
  • 它说它编写了一个完整的 Seam 应用程序,所以我会说这是真的,并建议 Roo 作为 Spring 的替代品,但我找不到非 Spring/Seam 插件......有点难过... :) 无论如何,这是目前最好的答案...
【解决方案2】:

Telosys Tools 可以同时生成:JPA 实体和 DTO

一起来看看这个教程https://sites.google.com/site/telosystutorial/springmvc-jpa-springdatajpa

它使用 JPA 生成一个完整的 Spring MVC CRUD 应用程序 架构:https://sites.google.com/site/telosystutorial/springmvc-jpa-springdatajpa/presentation/architecture 映射器Entity/DTO也被生成(它使用“org.modelmapper”)

模板是可定制的

【讨论】:

    【解决方案3】:

    尝试查看: https://github.com/nikelin/spring-data-generation-kit

    但它只适合你,如果你的项目是在 Maven 控制。

    【讨论】:

      猜你喜欢
      • 2011-06-25
      • 1970-01-01
      • 2017-02-02
      • 2011-11-05
      • 2018-10-30
      • 2014-07-23
      • 2011-04-12
      • 1970-01-01
      • 2021-08-03
      相关资源
      最近更新 更多