适用于JPA实体的DTO生成器

时间:2011-12-13 19:23:12

标签: java hibernate jpa

有人能告诉我是否有JPA实体的DTO生成器? 谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

如果您使用的是eclipse,可能Hibernate Tools应该这样做:http://hibernate.org/subprojects/tools.html

答案 1 :(得分:0)

我开发了一个 apt 库 beanknife。 提供实体

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
    @Id
    private String number;
    private String name;
    private String sex;
    private String nation;
    @Temporal(TemporalType.DATE)
    private Date birthDay;
    @Temporal(TemporalType.DATE)
    private Date enrollmentDay;
    @ManyToOne
    private Department department;
    @ManyToOne
    private Company company;
}

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Department {
    @Id
    private String number;
    @ManyToOne
    private Company company;
    @OneToMany(mappedBy = "department")
    private List<Employee> employees;
}

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Company {
    @Id
    private String code;
    private String name;
    private Double money;
    private Address address;
    @OneToMany(mappedBy = "company")
    private List<Department> departments;
    @OneToMany(mappedBy = "company")
    private List<Employee> employees;
}

还有一些配置类

@ViewPropertiesIncludePattern(".*")
@ViewGenNameMapper("${name}Info")
@AddJpaSupport
public class BaseDtoConfiguration {
}

@ViewOf(Employee.class)
@RemoveViewProperty({EmployeeMeta.company, EmployeeMeta.department})
public class EmployeeInfoConfiguration extends BaseDtoConfiguration {
}

@ViewOf(value = Employee.class, genName = "EmployeeDetail")
public class EmployeeDetailConfiguration extends BaseDtoConfiguration {

    @MapViewProperty(name = "departmentInfo", map = "department")
    private DepartmentInfo departmentInfo;

    @MapViewProperty(name = "companyInfo", map = "company")
    private CompanyInfo companyInfo;
}

@ViewOf(Department.class)
@RemoveViewProperty(DepartmentMeta.company)
public class DepartmentInfoConfiguration extends BaseDtoConfiguration {

    @OverrideViewProperty(DepartmentMeta.employees)
    private List<EmployeeInfo> employees;
}

@ViewOf(Company.class)
@RemoveViewProperty({CompanyMeta.departments, CompanyMeta.employees})
public class CompanyInfoConfiguration extends BaseDtoConfiguration {

    @OverrideViewProperty(CompanyMeta.money)
    @NullNumberAsZero
    private double money;
}

会为你生成dtos

@GeneratedView(targetClass = Employee.class, configClass = EmployeeInfoConfiguration.class)
public class EmployeeInfo {

    private String number;

    private String name;

    private String sex;

    private String nation;

    private Date birthDay;

    private Date enrollmentDay;

    public EmployeeInfo() { }

    public EmployeeInfo(
        String number,
        String name,
        String sex,
        String nation,
        Date birthDay,
        Date enrollmentDay
    )  { ... }

    public EmployeeInfo(EmployeeInfo source) { ... }

    public EmployeeInfo(Employee source) { ... }

    public static EmployeeInfo read(Employee source) {
        if (source == null) {
            return null;
        }
        return new EmployeeInfo(source);
    }

    /** 
      * getter methods and other methods...
     **/
}

@GeneratedView(targetClass = Employee.class, configClass = EmployeeDetailConfiguration.class)
public class EmployeeDetail {

    private String number;

    private String name;

    private String sex;

    private String nation;

    private Date birthDay;

    private Date enrollmentDay;

    private DepartmentInfo departmentInfo;

    private CompanyInfo companyInfo;

    // other constructors

    public EmployeeDetail(Employee source) {
        if (source == null) {
            throw new NullPointerException("The input source argument of the read constructor of class io.github.vipcxj.beanknfie.jpa.examples.models.EmployeeDetail should not be null.");
        }
        DepartmentInfo p0 = DepartmentInfo.read(source.getDepartment());
        CompanyInfo p1 = CompanyInfo.read(source.getCompany());
        this.number = source.getNumber();
        this.name = source.getName();
        this.sex = source.getSex();
        this.nation = source.getNation();
        this.birthDay = source.getBirthDay();
        this.enrollmentDay = source.getEnrollmentDay();
        this.departmentInfo = p0;
        this.companyInfo = p1;
    }

    public static EmployeeDetail read(Employee source) {
        if (source == null) {
            return null;
        }
        return new EmployeeDetail(source);
    }

    public EmployeeDetail (
        String number,
        String name,
        String sex,
        String nation,
        Date birthDay,
        Date enrollmentDay,
        Department department,
        String companyCode,
        String companyName,
        Double companyMoney,
        Address companyAddress
    ) {
        DepartmentInfo viewVar0 = DepartmentInfo.read(department);
        CompanyInfo viewVar1 = new CompanyInfo(companyCode, companyName, new io.github.vipcxj.beanknife.runtime.converters.NullDoubleAsZeroConverter().convert(companyMoney), companyAddress);
        this.number = number;
        this.name = name;
        this.sex = sex;
        this.nation = nation;
        this.birthDay = birthDay;
        this.enrollmentDay = enrollmentDay;
        this.departmentInfo = viewVar0;
        this.companyInfo = viewVar1;
    }

    public static <T> Selection<EmployeeDetail> toJpaSelection(CriteriaBuilder cb, From<T, Employee> from) {
        return cb.construct(
            EmployeeDetail.class,
            from.get("number"),
            from.get("name"),
            from.get("sex"),
            from.get("nation"),
            from.get("birthDay"),
            from.get("enrollmentDay"),
            from.get("department"),
            from.get("company").get("code"),
            from.get("company").get("name"),
            from.get("company").get("money"),
            from.get("company").get("address")
        );
    }
}

@GeneratedView(targetClass = Department.class, configClass = DepartmentInfoConfiguration.class)
public class DepartmentInfo {

    private String number;

    private List<EmployeeInfo> employees;

    // other constructors

    public DepartmentInfo(Department source) {
        if (source == null) {
            throw new NullPointerException("The input source argument of the read constructor of class io.github.vipcxj.beanknfie.jpa.examples.models.DepartmentInfo should not be null.");
        }
        List<EmployeeInfo> p0 = new ArrayList<>();
        for (Employee el0 : source.getEmployees()) {
            EmployeeInfo result0 = EmployeeInfo.read(el0);
            p0.add(result0);
        }
        this.number = source.getNumber();
        this.employees = p0;
    }

    public static DepartmentInfo read(Department source) {
        if (source == null) {
            return null;
        }
        return new DepartmentInfo(source);
    }

    // other methods and getters

    public DepartmentInfo (Department source, int preventConflictArg) {
        List<EmployeeInfo> viewVar0 = EmployeeInfo.read(source.getEmployees());
        this.number = source.getNumber();
        this.employees = viewVar0;
    }

    public static <T> Selection<DepartmentInfo> toJpaSelection(CriteriaBuilder cb, From<T, Department> from) {
        return cb.construct(
            DepartmentInfo.class,
            from,
            cb.literal(0)
        );
    }
}

@GeneratedView(targetClass = Company.class, configClass = CompanyInfoConfiguration.class)
public class CompanyInfo {

    private String code;

    private String name;

    private double money;

    private Address address;

    // other constructors

    public CompanyInfo(Company source) {
        if (source == null) {
            throw new NullPointerException("The input source argument of the read constructor of class io.github.vipcxj.beanknfie.jpa.examples.models.CompanyInfo should not be null.");
        }
        this.code = source.getCode();
        this.name = source.getName();
        this.money = new NullDoubleAsZeroConverter().convert(source.getMoney());
        this.address = source.getAddress();
    }

    public static CompanyInfo read(Company source) {
        if (source == null) {
            return null;
        }
        return new CompanyInfo(source);
    }

    // other read methods

    public CompanyInfo (String code, String name, Double money, Address address) {
        this.code = code;
        this.name = name;
        this.money = new NullDoubleAsZeroConverter().convert(money);
        this.address = address;
    }

    public static <T> Selection<CompanyInfo> toJpaSelection(CriteriaBuilder cb, From<T, Company> from) {
        return cb.construct(
            CompanyInfo.class,
            from.get("code"),
            from.get("name"),
            from.get("money"),
            from.get("address")
        );
    }
}

Beanknife 有插件支持。使用 jpa 插件,它也会生成一个 toJpaSelection() 方法。 使用它

        CriteriaQuery<EmployeeDetail> query = cb.createQuery(EmployeeDetail.class);
        Root<Employee> employees = query.from(Employee.class);
        Selection<EmployeeDetail> employeeInfoSelection = EmployeeDetail.toJpaSelection(cb, employees);
        List<EmployeeDetail> resultList = em.createQuery(query.select(employeeInfoSelection)).getResultList();

整个例子是here