Hibernate分组和结果集转换

时间:2015-02-24 11:07:04

标签: sql hibernate grouping hibernate-mapping

假设我有以下结果集:

enter image description here

所以,我们在这里有一对多的关系。 问题是在Hibernate中将它分组并将此数据结构转换为DTO的最佳方法是:

String countryName String companyName List invoiceNumbers

谢谢!

1 个答案:

答案 0 :(得分:0)

CountryCompany ---- Many-to-Many 对于公司和发票--- One-to-Many

@Entity
public class Country {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

@ManyToMany(mappedBy="countries")
private Collection<Country> countries;
...
getters and setters

}

@Entity
public class Company {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

@ManyToMany
private Collection<Country> countries;

@OneToMany
private Collection<Invoice> invoices; 
...
getters and setters

}

@Entity
public class Invoice {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

private int invoice_number;
...
getters and setters

}