JpaRepository返回包含重复对象的列表

时间:2015-12-10 11:15:11

标签: java spring jpa

我正在试图弄清楚如何从jpa存储库中正确获取2个对象

现在这是我的存储库:

@Transactional
public interface CompanyDao extends JpaRepository<Company, Integer> {
    //HQL query
    @Query("SELECT e,b from EstimateOptions e,BillNumber b inner join e.Company company inner join company.user user where user.email = :userName\n")
    List <Object[]>testQuery(@Param("userName") String userName);
}

但是当我在Controller中测试它时

@RequestMapping(value = "/getcompanyestimateoptions")
public List getCompanyEstimateOptions(@AuthenticationPrincipal Principal user) {



    LOGGER.info("TEST LIST");
    List<Object[]> listObjects = companyDao.testQuery(user.getName());

    for (Object object[] : listObjects) {
        EstimateOptions estimateOptions = (EstimateOptions) object[0];
        BillNumber billNumber = (BillNumber) object[1];

        LOGGER.info(estimateOptions.toString());
        LOGGER.info(billNumber.toString());
        LOGGER.info(object.toString());
    }

    LOGGER.info("START ESTIMATEOPTIONS QUERY");


    return listObjects;

}

实体用户

public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_user")
    private Integer id;

    @OneToOne( cascade = CascadeType.ALL)
    @JoinColumn(name = "fkIdCompanyUser")
    private Company company = new Company();
}

实体号码

@Entity
@Table(name ="tbl_BillNumber")
public class BillNumber {

    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(UserController.class);


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_BillNumber")
    private Integer id;
}

实体估算选项

@Entity
@Table(name ="tbl_Estimateoptions")
public class EstimateOptions {

    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(EstimateOptions.class);


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_Estimateoptions")
    private Integer id;
}

实体公司

@Entity
@Table(name ="tbl_Company")
public class Company {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_company")
    private Integer id;

    @OneToOne(mappedBy="company", fetch = FetchType.LAZY)
    private User user;

    @OneToOne( cascade = CascadeType.ALL)
    @JoinColumn(name = "fkIdCompanyBill")
    private BillNumber billnumber = new BillNumber();

    @OneToOne( cascade = CascadeType.ALL)
    @JoinColumn(name = "fkIdCompanyEstimateOption")
    private EstimateOptions estimateOptions = new EstimateOptions();
}

这是sql:

的输出
Hibernate: select distinct estimateop0_.id_estimateoptions as id_estim1_6_0_, billnumber1_.id_bill_number as id_bill_1_0_1_, estimateop0_.estimate_customer_note as estimate2_6_0_, estimateop0_.estimate_model as estimate3_6_0_, estimateop0_.estimate_model_primary_color as estimate4_6_0_, estimateop0_.estimate_model_secondary_color as estimate5_6_0_, estimateop0_.estimate_model_tertiary_color as estimate6_6_0_, estimateop0_.estimate_personal_note as estimate7_6_0_, estimateop0_.estimate_terms as estimate8_6_0_, billnumber1_.credit_note_numberprefix as credit_n2_0_1_, billnumber1_.credit_note_numbersuffix as credit_n3_0_1_, billnumber1_.credit_note_start_number as credit_n4_0_1_, billnumber1_.customers_id_on as customer5_0_1_, billnumber1_.estimate_numberprefix as estimate6_0_1_, billnumber1_.estimate_numbersuffix as estimate7_0_1_, billnumber1_.estimate_start_number as estimate8_0_1_, billnumber1_.every_year_new_date_on as every_ye9_0_1_, billnumber1_.invoice_numberprefix as invoice10_0_1_, billnumber1_.invoice_numbersuffix as invoice11_0_1_, billnumber1_.invoice_project_number as invoice12_0_1_, billnumber1_.invoice_start_number as invoice13_0_1_, billnumber1_.month_on as month_o14_0_1_, billnumber1_.project_number_numberprefix as project15_0_1_, billnumber1_.project_number_on as project16_0_1_, billnumber1_.project_number_total_prefix_numbers as project17_0_1_, billnumber1_.total_prefix_numbers as total_p18_0_1_, billnumber1_.year_on as year_on19_0_1_ from tbl_estimateoptions estimateop0_ inner join tbl_company company2_ on estimateop0_.id_estimateoptions=company2_.fk_id_company_estimate_option inner join tbl_user user3_ on company2_.id_company=user3_.fk_id_company_user cross join tbl_bill_number billnumber1_ where user3_.email=?
Hibernate: select company0_.id_company as id_compa1_2_2_, company0_.bank_account as bank_acc2_2_2_, company0_.fk_id_company_bill as fk_id_c18_2_2_, company0_.city as city3_2_2_, company0_.company_name as company_4_2_2_, company0_.contact_email as contact_5_2_2_, company0_.contact_telephone as contact_6_2_2_, company0_.country as country7_2_2_, company0_.dateformat as dateform8_2_2_, company0_.default_currency as default_9_2_2_, company0_.fk_id_company_estimate_option as fk_id_c19_2_2_, company0_.hide_tax_number_on_estimate as hide_ta10_2_2_, company0_.house_number as house_n11_2_2_, company0_.street as street12_2_2_, company0_.tax_number as tax_num13_2_2_, company0_.tax_number_enabled as tax_num14_2_2_, company0_.website as website15_2_2_, company0_.website_enabled as website16_2_2_, company0_.zip_code as zip_cod17_2_2_, billnumber1_.id_bill_number as id_bill_1_0_0_, billnumber1_.credit_note_numberprefix as credit_n2_0_0_, billnumber1_.credit_note_numbersuffix as credit_n3_0_0_, billnumber1_.credit_note_start_number as credit_n4_0_0_, billnumber1_.customers_id_on as customer5_0_0_, billnumber1_.estimate_numberprefix as estimate6_0_0_, billnumber1_.estimate_numbersuffix as estimate7_0_0_, billnumber1_.estimate_start_number as estimate8_0_0_, billnumber1_.every_year_new_date_on as every_ye9_0_0_, billnumber1_.invoice_numberprefix as invoice10_0_0_, billnumber1_.invoice_numbersuffix as invoice11_0_0_, billnumber1_.invoice_project_number as invoice12_0_0_, billnumber1_.invoice_start_number as invoice13_0_0_, billnumber1_.month_on as month_o14_0_0_, billnumber1_.project_number_numberprefix as project15_0_0_, billnumber1_.project_number_on as project16_0_0_, billnumber1_.project_number_total_prefix_numbers as project17_0_0_, billnumber1_.total_prefix_numbers as total_p18_0_0_, billnumber1_.year_on as year_on19_0_0_, estimateop2_.id_estimateoptions as id_estim1_6_1_, estimateop2_.estimate_customer_note as estimate2_6_1_, estimateop2_.estimate_model as estimate3_6_1_, estimateop2_.estimate_model_primary_color as estimate4_6_1_, estimateop2_.estimate_model_secondary_color as estimate5_6_1_, estimateop2_.estimate_model_tertiary_color as estimate6_6_1_, estimateop2_.estimate_personal_note as estimate7_6_1_, estimateop2_.estimate_terms as estimate8_6_1_ from tbl_company company0_ left outer join tbl_bill_number billnumber1_ on company0_.fk_id_company_bill=billnumber1_.id_bill_number left outer join tbl_estimateoptions estimateop2_ on company0_.fk_id_company_estimate_option=estimateop2_.id_estimateoptions where company0_.fk_id_company_estimate_option=?
Hibernate: select user0_.id_user as id_user1_9_3_, user0_.authority as authorit2_9_3_, user0_.fk_id_company_user as fk_id_co8_9_3_, user0_.email as email3_9_3_, user0_.enabled as enabled4_9_3_, user0_.first_name as first_na5_9_3_, user0_.last_name as last_nam6_9_3_, user0_.password as password7_9_3_, company1_.id_company as id_compa1_2_0_, company1_.bank_account as bank_acc2_2_0_, company1_.fk_id_company_bill as 

该列表包含BillNumberEstimateOptions两次。因此,当我将其发送到前端时,我得到了包含2个重复信息的数组。我做错了什么?

修改

使用以下查询时

@Query("SELECT distinct  e,b from EstimateOptions e,BillNumber b join fetch e.Company company join fetch company.user user where user.email = :userName\n")
List <Object[]>testQuery(@Param("userName") String userName);

SQL输出现在是:

 Hibernate: select distinct estimateop0_.id_estimateoptions as id_estim1_6_0_, billnumber1_.id_bill_number as id_bill_1_0_1_, company2_.id_company as id_compa1_2_2_, user3_.id_user as id_user1_9_3_, estimateop0_.estimate_customer_note as estimate2_6_0_, estimateop0_.estimate_model as estimate3_6_0_, estimateop0_.estimate_model_primary_color as estimate4_6_0_, estimateop0_.estimate_model_secondary_color as estimate5_6_0_, estimateop0_.estimate_model_tertiary_color as estimate6_6_0_, estimateop0_.estimate_personal_note as estimate7_6_0_, estimateop0_.estimate_terms as estimate8_6_0_, billnumber1_.credit_note_numberprefix as credit_n2_0_1_, billnumber1_.credit_note_numbersuffix as credit_n3_0_1_, billnumber1_.credit_note_start_number as credit_n4_0_1_, billnumber1_.customers_id_on as customer5_0_1_, billnumber1_.estimate_numberprefix as estimate6_0_1_, billnumber1_.estimate_numbersuffix as estimate7_0_1_, billnumber1_.estimate_start_number as estimate8_0_1_, billnumber1_.every_year_new_date_on as every_ye9_0_1_, billnumber1_.invoice_numberprefix as invoice10_0_1_, billnumber1_.invoice_numbersuffix as invoice11_0_1_, billnumber1_.invoice_project_number as invoice12_0_1_, billnumber1_.invoice_start_number as invoice13_0_1_, billnumber1_.month_on as month_o14_0_1_, billnumber1_.project_number_numberprefix as project15_0_1_, billnumber1_.project_number_on as project16_0_1_, billnumber1_.project_number_total_prefix_numbers as project17_0_1_, billnumber1_.total_prefix_numbers as total_p18_0_1_, billnumber1_.year_on as year_on19_0_1_, company2_.bank_account as bank_acc2_2_2_, company2_.fk_id_company_bill as fk_id_c18_2_2_, company2_.city as city3_2_2_, company2_.company_name as company_4_2_2_, company2_.contact_email as contact_5_2_2_, company2_.contact_telephone as contact_6_2_2_, company2_.country as country7_2_2_, company2_.dateformat as dateform8_2_2_, company2_.default_currency as default_9_2_2_, company2_.fk_id_company_estimate_option as fk_id_c19_2_2_, company2_.hide_tax_number_on_estimate as hide_ta10_2_2_, company2_.house_number as house_n11_2_2_, company2_.street as street12_2_2_, company2_.tax_number as tax_num13_2_2_, company2_.tax_number_enabled as tax_num14_2_2_, company2_.website as website15_2_2_, company2_.website_enabled as website16_2_2_, company2_.zip_code as zip_cod17_2_2_, user3_.authority as authorit2_9_3_, user3_.fk_id_company_user as fk_id_co8_9_3_, user3_.email as email3_9_3_, user3_.enabled as enabled4_9_3_, user3_.first_name as first_na5_9_3_, user3_.last_name as last_nam6_9_3_, user3_.password as password7_9_3_ from tbl_estimateoptions estimateop0_ inner join tbl_company company2_ on estimateop0_.id_estimateoptions=company2_.fk_id_company_estimate_option inner join tbl_user user3_ on company2_.id_company=user3_.fk_id_company_user cross join tbl_bill_number billnumber1_ where user3_.email=?

但该列表仍包含重复信息。

1 个答案:

答案 0 :(得分:1)

我找到了解决此问题的可能方案。最后,我重新配置了这样的查询。现在我得到一个没有公司或用户所有设置的EstimateOptions和BillNumber的对象。也没有更多重复的对象。

It simply returns a string and not an Object's property to the template

但我的查询中仍然有一些重复的信息

@Query("from EstimateOptions options,BillNumber billnumber join fetch billnumber.Company company2 join fetch company2.user user2  join fetch options.Company company join fetch company.user user where user.email = :userName and user2.email = :userName")
Set<Object[]> testQuery6(@Param("userName") String userName);

我不接受我的回答,因为可能有更好的方法来解决这个问题。但它更像是解决这个问题。请随时留下评论,以便进一步改进。