Hibernate中的ManyToMany关系

时间:2019-04-17 15:36:29

标签: sql hibernate jpa spring-data-jpa hql

我有这样的域:

  

ParameterValue

     

公共类ParameterValue扩展StatefulEntity {

@Id
@GeneratedValue(generator = Constants.SequenceGenerator.PARAMETER_VALUE,
        strategy = GenerationType.SEQUENCE)
@SequenceGenerator(
        sequenceName = Constants.Sequence.PARAMETER_VALUE_SEQ,
        name = Constants.SequenceGenerator.PARAMETER_VALUE,
        allocationSize = 1
)
private Long id;

@Column
private BigDecimal rangeStart;

@Column
private BigDecimal rangeEnd;

@Column
private LocalDateTime timeStart;

@Column
private LocalDateTime timeEnd;

@Column
private String mdmDictCode;

@Column
private String mdmDictFieldId;

@Column
private String mdmDictFieldCode;

@Column
private String mdmValue;

@Column
private String textValue;

@Column
private BigDecimal numberValue;

@ManyToOne(fetch = FetchType.LAZY,
        cascade = {CascadeType.REFRESH, CascadeType.DETACH})
private Parameter parameter;

@JsonIgnore
@ManyToMany(mappedBy = "parameterValues")
private List<RateValue> rateValues = new ArrayList<>(); }

参数:

  

公共类参数扩展StatefulEntity {

@Id
@GeneratedValue(generator = Constants.SequenceGenerator.PARAMETER,
        strategy = GenerationType.SEQUENCE)
@SequenceGenerator(
        sequenceName = Constants.Sequence.PARAMETER_SEQ,
        name = Constants.SequenceGenerator.PARAMETER,
        allocationSize = 1
)
private Long id;

@Column
private String code;

@Column
private String description;

@Column
private String mdmDictId;

@Column
private String mdmDictFieldId;

@Column
private String mdmDictFieldCode;

@ManyToOne(fetch = FetchType.LAZY, optional = false,
        cascade = {CascadeType.DETACH, CascadeType.REFRESH})
private ParameterType parameterType;

@Column
private Boolean nullable;

@JsonIgnore
@OneToMany(mappedBy = "parameter")
private List<ParameterValue> parameterValues = new ArrayList<>();

@JsonIgnore
@ManyToMany(mappedBy = "parameters")
private List<Rate> rates = new ArrayList<>(); }

Rate_value

public class RateValue extends StatefulEntity{

    @Id
    @GeneratedValue(generator = Constants.SequenceGenerator.RATE_VALUE,
                    strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(
            sequenceName = Constants.Sequence.RATE_VALUE_SEQ,
            name = Constants.SequenceGenerator.RATE_VALUE,
            allocationSize = 1
    )
    private Long id;

    @JsonIgnore
    @JoinColumn(name = "rate_id")
    @ManyToOne(fetch = FetchType.LAZY, optional = false,
            cascade = {CascadeType.DETACH, CascadeType.REFRESH})
    private Rate rate;

    @Column
    private String currencyCode;

    @Column
    @NotNull
    private BigDecimal interestValue;

    @Column
    private Boolean isInterest = false;

    @Column
    private BigDecimal minValue;

    @Column
    private BigDecimal maxValue;

    @Column
    private Boolean isDefaultValue = false;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "rate_rate_pack_link_id")
    private RateRatePackLink rateRatePackLink;

    // ссылка на параметры
    @ManyToMany(
            fetch = FetchType.LAZY,
            cascade = {CascadeType.REFRESH, CascadeType.DETACH}
    )
    @JoinTable(name = Constants.Table.RATE_PARAMETER_VALUE,
            schema = Schema.RATES,
            joinColumns = {
                    @JoinColumn(name = Constants.Column.RATE_VALUE_ID,
                            referencedColumnName = Constants.Column.ID)
            },
            inverseJoinColumns = {
                    @JoinColumn(name = Constants.Column.PARAMETER_VALUE_ID,
                            referencedColumnName = Constants.Column.ID)
            }
    )
    private List<ParameterValue> parameterValues = new ArrayList<>();

    @Column
    @NotNull
    private String branchValue;

}

状态

  

@Data @Audited @Entity @Table(           名称= Constants.Table.STATUS,           schema = Schema.RATES,           uniqueConstraints = @UniqueConstraint(                   名称= Constants.UniqueConstraint.STATUS_CODE,columnNames = Constants.Column.CODE           ))@EqualsAndHashCode(callSuper = false)公共类状态扩展GenericObject {

@Id
@GeneratedValue(generator = Constants.SequenceGenerator.STATUS,
                strategy = GenerationType.SEQUENCE)
@SequenceGenerator(
        sequenceName = Sequence.STATUS_SEQ,
        name = Constants.SequenceGenerator.STATUS,
        allocationSize = 1
)
private Long id;

@Column(nullable = false, unique = true)
protected String code;

@Column
protected String description; }

我要选择所有不包含的RateValues rateValue.parameterValue.parameter.status =“ active”,如果RateValues没有activeparameters,则查询应返回不带参数的RateValues。

  @Query(value = "FROM RateValue r "
            + "LEFT JOIN FETCH r.status "
            + "LEFT JOIN FETCH r.parameterValues v "
            + "LEFT JOIN FETCH v.parameter p "
            + "WHERE v.parameter.status.code= 'ACTIVE'",

当前,该查询向我返回所有具有parameterValue和parameter.status = ACTIVE的RateValues。

但是我需要此rateValue的所有RateValues和parameterValues,它们的参数状态为ACTIVE

0 个答案:

没有答案