有没有办法(例如Eclipse插件)自动从实体(JPA)生成DTO?
问题描述:
我想要一个简单的前向DTO生成工具,它将
I would like a plain forward DTO generation tool that would either
- 生成它(例如cglib - 创建类和或者一个Eclipse插件,它将使用Entity并生成一个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界面中删除它:
If we don't want to include children in the graph, remove it from the DTO interface:
public interface MyEntityDTO {
public String getId();
public String getName();
public Long getRelatedId();
...
我确定有一些eclipse插件如果没有,我挑战某人写一个,或解释为什么我想要的是没有帮助的(并提供另一个建议)
I'm sure there is some eclipse plugn for it and if not, I challange someone to write one, or explain why what I want is not helpful (and provide an alternative suggestion)
答
可能Hibernate工具应该这样做: http://hibernate.org/subprojects/tools.html
Probably Hibernate Tools should be doing this: http://hibernate.org/subprojects/tools.html