java - JpaRepository 返回一个包含重复对象的列表

标签 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=?

但列表中仍然包含重复的信息。

最佳答案

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

@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);

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

Hibernate: select estimateop0_.id_estimateoptions as id_estim1_6_0_, company4_.id_company as id_compa1_2_1_, user5_.id_user as id_user1_9_2_, billnumber1_.id_bill_number as id_bill_1_0_3_, company2_.id_company as id_compa1_2_4_, user3_.id_user as id_user1_9_5_, 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_, company4_.bank_account as bank_acc2_2_1_, company4_.fk_id_company_bill as fk_id_c18_2_1_, company4_.city as city3_2_1_, company4_.company_name as company_4_2_1_, company4_.contact_email as contact_5_2_1_, company4_.contact_telephone as contact_6_2_1_, company4_.country as country7_2_1_, company4_.dateformat as dateform8_2_1_, company4_.default_currency as default_9_2_1_, company4_.fk_id_company_estimate_option as fk_id_c19_2_1_, company4_.hide_tax_number_on_estimate as hide_ta10_2_1_, company4_.house_number as house_n11_2_1_, company4_.street as street12_2_1_, company4_.tax_number as tax_num13_2_1_, company4_.tax_number_enabled as tax_num14_2_1_, company4_.website as website15_2_1_, company4_.website_enabled as website16_2_1_, company4_.zip_code as zip_cod17_2_1_, user5_.authority as authorit2_9_2_, user5_.fk_id_company_user as fk_id_co8_9_2_, user5_.email as email3_9_2_, user5_.enabled as enabled4_9_2_, user5_.first_name as first_na5_9_2_, user5_.last_name as last_nam6_9_2_, user5_.password as password7_9_2_, billnumber1_.credit_note_numberprefix as credit_n2_0_3_, billnumber1_.credit_note_numbersuffix as credit_n3_0_3_, billnumber1_.credit_note_start_number as credit_n4_0_3_, billnumber1_.customers_id_on as customer5_0_3_, billnumber1_.estimate_numberprefix as estimate6_0_3_, billnumber1_.estimate_numbersuffix as estimate7_0_3_, billnumber1_.estimate_start_number as estimate8_0_3_, billnumber1_.every_year_new_date_on as every_ye9_0_3_, billnumber1_.invoice_numberprefix as invoice10_0_3_, billnumber1_.invoice_numbersuffix as invoice11_0_3_, billnumber1_.invoice_project_number as invoice12_0_3_, billnumber1_.invoice_start_number as invoice13_0_3_, billnumber1_.month_on as month_o14_0_3_, billnumber1_.project_number_numberprefix as project15_0_3_, billnumber1_.project_number_on as project16_0_3_, billnumber1_.project_number_total_prefix_numbers as project17_0_3_, billnumber1_.total_prefix_numbers as total_p18_0_3_, billnumber1_.year_on as year_on19_0_3_, company2_.bank_account as bank_acc2_2_4_, company2_.fk_id_company_bill as fk_id_c18_2_4_, company2_.city as city3_2_4_, company2_.company_name as company_4_2_4_, company2_.contact_email as contact_5_2_4_, company2_.contact_telephone as contact_6_2_4_, company2_.country as country7_2_4_, company2_.dateformat as dateform8_2_4_, company2_.default_currency as default_9_2_4_, company2_.fk_id_company_estimate_option as fk_id_c19_2_4_, company2_.hide_tax_number_on_estimate as hide_ta10_2_4_, company2_.house_number as house_n11_2_4_, company2_.street as street12_2_4_, company2_.tax_number as tax_num13_2_4_, company2_.tax_number_enabled as tax_num14_2_4_, company2_.website as website15_2_4_, company2_.website_enabled as website16_2_4_, company2_.zip_code as zip_cod17_2_4_, user3_.authority as authorit2_9_5_, user3_.fk_id_company_user as fk_id_co8_9_5_, user3_.email as email3_9_5_, user3_.enabled as enabled4_9_5_, user3_.first_name as first_na5_9_5_, user3_.last_name as last_nam6_9_5_, user3_.password as password7_9_5_ from tbl_estimateoptions estimateop0_ inner join tbl_company company4_ on estimateop0_.id_estimateoptions=company4_.fk_id_company_estimate_option inner join tbl_user user5_ on company4_.id_company=user5_.fk_id_company_user cross join tbl_bill_number billnumber1_ inner join tbl_company company2_ on billnumber1_.id_bill_number=company2_.fk_id_company_bill inner join tbl_user user3_ on company2_.id_company=user3_.fk_id_company_user where user5_.email=? and 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_bill=?

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

关于java - JpaRepository 返回一个包含重复对象的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34200488/

相关文章:

java - 如何在java中获取特定日期UTC值

orm - 您如何使用 Eclipse 链接获取然后释放直接数据库连接

java - 将 @EmbeddedId 与 JpaRepository 一起使用

java - 在 update() void 中添加 if 语句时,图形不出现

java - 当文件夹不为空时 file.exists 返回 false

Spring 安全注销处理程序不工作

java - ApplicationContextInitializer 和 BeanFactoryPostProcessor 之间的区别

spring - 如何在用@Entity注释的类中使用@Autowired?

java - 安装Tomcat + Solr问题

java - Spring JPA - 如何在事务中强制自定义存储库调用顺序