【问题标题】:Hibernate grouping and result set transformationHibernate 分组和结果集转换
【发布时间】:2015-02-24 11:07:03
【问题描述】:

假设我有以下结果集:

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

String countryName String companyName List invoiceNumbers

谢谢!

【问题讨论】:

    标签: sql hibernate grouping hibernate-mapping


    【解决方案1】:

    对于Country 和Company ---- 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
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-07-21
      • 2017-05-03
      • 2021-03-10
      • 2012-01-20
      • 1970-01-01
      • 2016-09-15
      • 1970-01-01
      • 2011-06-17
      • 1970-01-01
      相关资源
      最近更新 更多