如何使用Hibernate中的Root元素从父实体中选择子属性

问题描述

我正在处理具有多个子实体的Master实体。我需要使用Master实体中的root元素来选择子元素。 让我发布实体结构

public class MasterEntity {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private BigInteger masterId;

private String masterNumber;


private Integer masterName;

private boolean MasterFlag;

@OnetoOne(cascade = CascadeType.ALL)
@JoinColumn(name = "as_slave1_account_id")
private MappingFirst mappingFirst;

@OnetoOne(cascade = CascadeType.ALL)
@JoinColumn(name = "as_slave2_account_id")
private MappingSecond mappingSecond;

@OnetoOne(cascade = CascadeType.ALL)
@JoinColumn(name = "as_slave3_account_id")
private MappingThird mappingThird;




@PrePersist
private void prePersist() {
    if (this.createdOn == null) this.createdOn = OffsetDateTime.Now();
}

@PreUpdate
private void preUpdate() {
    this.lastModifiedOn = OffsetDateTime.Now();
}


}

一个子实体

public class MappingFirst  {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long mappingFirstId;

@NotNull(message = "account_number can't be null")
private String accountNumber;

@ManyToOne( fetch = FetchType.EAGER )
@JoinColumn( name = "as_drill_down_id",referencedColumnName = "asDrillDownId")
@NotNull
private DrilDownOne drilDownOne;
    
}

第二个子实体

public class DrilDownOne{

@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
private long asDillDownId;

}

我正在发布要从根中提取元素的类

public class MasterElementSearchCriteria implements Consumer<SearchCriteria> {
private Predicate predicate;
private CriteriaBuilder builder;
private Root rootElement;

@Override
public void accept(SearchCriteria param) {

    if (param.getValue() != null && param.getValue() != "") {
        if (param.getoperation().equalsIgnoreCase(CriteriaOperationEnum.GREATER_THAN_OR_EQUAL_TO.getoperation())) {
            if (rootElement.get(param.getKey()).getJavaType() == LocalDate.class) {
                predicate = builder.and(predicate,builder.greaterThanorEqualTo(rootElement.get(param.getKey()).as(LocalDate.class),(LocalDate) param.getValue()));
            } else {
                predicate = builder.and(predicate,builder.greaterThanorEqualTo(rootElement.get(param.getKey()),param.getValue().toString()));
            }
        } else if (param.getoperation().equalsIgnoreCase(CriteriaOperationEnum.LESSER_THAN_OR_EQUAL_TO.getoperation())) {
            if (rootElement.get(param.getKey()).getJavaType() == LocalDate.class) {
                predicate = builder.and(predicate,builder.lessthanorEqualTo(rootElement.get(param.getKey()).as(LocalDate.class),builder.lessthanorEqualTo(rootElement.get(param.getKey()),param.getValue().toString())); // Here i am pulling the element from the root,but here i want to pull the asDillDownId of the  DrilDownOne Entity
            }
        } else if (param.getoperation().equalsIgnoreCase(CriteriaOperationEnum.EQUALS_IGnorE_CASE.getoperation())) {
            if (rootElement.get(param.getKey()).getJavaType() == String.class) {
                predicate = builder.and(predicate,builder.like(builder.upper(rootElement.get(param.getKey())),CriteriaOperationEnum.LIKE.getoperation() + param.getValue().toString().toupperCase() + CriteriaOperationEnum.LIKE.getoperation()));
            } else {
                predicate = builder.and(predicate,builder.equal(rootElement.get(param.getKey()),param.getValue()));
            }
        } else if (param.getoperation().equalsIgnoreCase(CriteriaOperationEnum.LIKE.getoperation())) {
            predicate = builder.and(predicate,CriteriaOperationEnum.LIKE.getoperation() + param.getValue().toString().toupperCase() + CriteriaOperationEnum.LIKE.getoperation()));
        }
    }
}

public Predicate getPredicate() {
    return predicate;
}
}

我的要求是,我想使用Root元素提取DrilDownOne实体信息。有没有办法做到这一点,请提出建议。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)