Создание запросов (CriteriaQuery) с объединениями с использованием наследования и спецификации данных Spring - PullRequest
0 голосов
/ 27 августа 2018

Я не могу сделать запрос, используя Spring Data Specification для получения моей сущности с наследованием. Я хотел бы знать, как выполнять объединения, используя функцию on () для добавления правил объединения.

Я бы хотел, чтобы результат выбора был:

select a.*
  from recharge a left join transfer_information b on 
  a.recharge_information_id = b.id
where b.bank_number = '237'

Моя сущность Пополнение

@Entity
public class Recharge {

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

@Embedded
private RechargeId rechargeId;

@OneToOne(cascade = CascadeType.ALL)
private RechargeInformation rechargeInformation;

//Constructor getters and setters

}

Мой общий класс

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class RechargeInformation {

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

@Embedded
private Money paidAmount;

}

И конкретный класс:

@Entity
@Table(name = "TRANSFER_INFORMATION")
public class TransferInformation extends RechargeInformation {

private String bankNumber;

//Constructor getters and setters
}

Моя спецификация класса

public static Specification<Recharge> bankNumber(String bank) {
    return (root, query, cb) -> {
        final Root<TransferInformation> transfer = query.from(TransferInformation.class);
        return cb.and(cb.equal(root.get("rechargeInformation").get("id"), transfer.get("id")),
                cb.equal(transfer.get("bankNumber"), bank));
    };
}

Это решение не работает, hibernate выполняет этот запрос в цикле, извлекая данные из двух конкретных классов, invoice_information и Transfer_information, когда правильный находится только в Transfer_information.

Hibernate: 
select
    rechargein0_.id as id1_4_0_,
    rechargein0_.paid_amount as paid_amo2_4_0_,
    rechargein0_.account as account1_5_0_,
    rechargein0_.agency as agency2_5_0_,
    rechargein0_.bank_number as bank_num3_5_0_,
    rechargein0_.change_user as change_u4_5_0_,
    rechargein0_.date_change as date_cha5_5_0_,
    rechargein0_.url_receipt as url_rece6_5_0_,
    rechargein0_.address_id as address13_2_0_,
    rechargein0_.bank as bank1_2_0_,
    rechargein0_.charge_id as charge_i2_2_0_,
    rechargein0_.document_number as document3_2_0_,
    rechargein0_.due_date as due_date4_2_0_,
    rechargein0_.instructions as instruct5_2_0_,
    rechargein0_.line as line6_2_0_,
    rechargein0_.owner_number as owner_nu7_2_0_,
    rechargein0_.sent as sent8_2_0_,
    rechargein0_.url as url9_2_0_,
    rechargein0_.url_bar_code as url_bar10_2_0_,
    rechargein0_.url_pdf as url_pdf11_2_0_,
    rechargein0_.urlqrcode as urlqrco12_2_0_,
    rechargein0_.clazz_ as clazz_0_,
    address1_.id as id1_0_1_,
    address1_.city as city2_0_1_,
    address1_.complement as compleme3_0_1_,
    address1_.country as country4_0_1_,
    address1_.neighborhood as neighbor5_0_1_,
    address1_.number as number6_0_1_,
    address1_.postal_code as postal_c7_0_1_,
    address1_.state as state8_0_1_,
    address1_.street as street9_0_1_ 
from
    ( select
        id,
        paid_amount,
        null::varchar as account,
        null::varchar as agency,
        null::varchar as bank_number,
        null::varchar as change_user,
        null::timestamp as date_change,
        null::varchar as url_receipt,
        null::varchar as bank,
        null::varchar as charge_id,
        null::varchar as document_number,
        null::timestamp as due_date,
        null::varchar as instructions,
        null::varchar as line,
        null::varchar as owner_number,
        null::boolean as sent,
        null::varchar as url,
        null::varchar as url_bar_code,
        null::varchar as url_pdf,
        null::varchar as urlqrcode,
        null::int8 as address_id,
        0 as clazz_ 
    from
        recharge_information 
    union
    all select
        id,
        paid_amount,
        account,
        agency,
        bank_number,
        change_user,
        date_change,
        url_receipt,
        null::varchar as bank,
        null::varchar as charge_id,
        null::varchar as document_number,
        null::timestamp as due_date,
        null::varchar as instructions,
        null::varchar as line,
        null::varchar as owner_number,
        null::boolean as sent,
        null::varchar as url,
        null::varchar as url_bar_code,
        null::varchar as url_pdf,
        null::varchar as urlqrcode,
        null::int8 as address_id,
        1 as clazz_ 
    from
        transfer_information 
    union
    all select
        id,
        paid_amount,
        null::varchar as account,
        null::varchar as agency,
        null::varchar as bank_number,
        null::varchar as change_user,
        null::timestamp as date_change,
        null::varchar as url_receipt,
        bank,
        charge_id,
        document_number,
        due_date,
        instructions,
        line,
        owner_number,
        sent,
        url,
        url_bar_code,
        url_pdf,
        urlqrcode,
        address_id,
        2 as clazz_ 
    from
        invoice_information 
) rechargein0_ 
  left outer join
    address address1_ 
         on rechargein0_.address_id=address1_.id 
where
  rechargein0_.id=?
...