我想要一个简单的DTO生成工具,
>即时生成(例如cglib-动态创建类和DTO对象)
>或将使用Entity并生成DTO的Eclipse插件(用户将指定要包含的树图,对于不包含的树图,则将包含外键而不是相关实体等)
例如.像这样
@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插件,如果没有,我会挑战某人写一个,或者解释为什么我想要的东西没有帮助(并提供替代建议)
解决方法:
可能是Hibernate Tools应该这样做:http://hibernate.org/subprojects/tools.html