java – eclipselink在子类中忽略AdditionalCriteria

如果我与父母和子女都设置了父/子关系,并且使用了@JoinFetch,那么将忽略childs additionalcriteria.

例如:

TableA.java

@javax.persistence.Entity
@Table(name = "TABLE_A")
@AdditionalCriteria("this.tableAfield2=:propA")
public class TableA {

    @Id
    @Column(name = "TABLEAFIELD1")
    private String tableAfield1;

    @Column(name = "TABLEAFIELD2")
    private String tableAfield2;

    @JoinColumn(name = "TABLEAFIELD2", referencedColumnName = "TABLEBFIELD1", insertable = false, updatable = false)  
    @OneToOne(fetch = FetchType.EAGER)    
//    @JoinFetch(JoinFetchType.OUTER)
    private TableB tableAtableB;
}

TableB.java:

@javax.persistence.Entity
@Table(name = "TABLE_B")
@AdditionalCriteria("this.tableBfield2=:propB")
public class TableB {

    @Id
    @Column(name = "TABLEBFIELD1")
    private String tableBfield1;

    @Column(name = "TABLEBFIELD2")
    private String tableBfield2;

    public String getTableBfield1() {
        return tableBfield1;
    }

    public String getTableBfield2() {
        return tableBfield2;
    }


}

主要:

        em.setProperty("propA", "propertyAValue");
        em.setProperty("propB", "propertyBValue");
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<TableA> criteriaQuery = cb.createQuery(TableA.class);
        Root<TableA> tableA = criteriaQuery.from(TableA.class);
        Predicate pred = cb.equal(tableA.get("tableAfield1"), "keyA1");
        criteriaQuery.where(pred);
        List<TableA> results = em.createQuery(criteriaQuery).getResultList();

使用tableA按照示例设置(注释掉JoinFetch)
应用程序创建2个SQL

SELECT TABLEAFIELD1, TABLEAFIELD2 FROM TABLE_A WHERE ((TABLEAFIELD1 = ?) AND (TABLEAFIELD2 = ?))
    bind => [keyA1, propertyAValue]

SELECT TABLEBFIELD1, TABLEBFIELD2 FROM TABLE_B WHERE ((TABLEBFIELD1 = ?) AND (TABLEBFIELD2 = ?))
    bind => [propertyAValue, propertyBValue]

这很好,因为eclipselink正在按需加载table_b.

但对于我们的应用程序,我们需要一个SQL,因为可能有1000行,我们需要一个连接.

所以,如果我放回@JoinFetch那么生成的sql是;

SELECT t1.TABLEAFIELD1, t1.TABLEAFIELD2, t0.TABLEBFIELD1, t0.TABLEBFIELD2 FROM TABLE_A t1 LEFT OUTER JOIN TABLE_B t0 ON (t0.TABLEBFIELD1 = t1.TABLEAFIELD2) WHERE ((t1.TABLEAFIELD1 = ?) AND (t1.TABLEAFIELD2 = ?))
    bind => [keyA1, propertyAValue]

不添加TableB中的additionalCriteria(没有t0.tableBField1 =?(propertyBValue))

有什么建议?它让我很生气.

非常感谢

为了完整起见,这里有表格

create table TABLE_A (
TABLEAFIELD1 varchar2(20),
TABLEAFIELD2 varchar2(30),
CONSTRAINT tableApk PRIMARY KEY (TABLEAFIELD1)
) ;

create table TABLE_B (
TABLEBFIELD1 varchar2(20),
TABLEBFIELD2 varchar2(30),
CONSTRAINT tableBpk PRIMARY KEY (TABLEBFIELD1)
) ;

insert into TABLE_A (TABLEAFIELD1,TABLEAFIELD2) values ('keyA1','propertyAValue');
insert into TABLE_A (TABLEAFIELD1,TABLEAFIELD2) values ('keyA2','propertyAValue');
insert into TABLE_A (TABLEAFIELD1,TABLEAFIELD2) values ('keyA3','random');

insert into TABLE_B (TABLEBFIELD1,TABLEBFIELD2) values ('propertyAValue','propertyBValue');
最佳答案
所以这是eclipselink的一个长期错误,看起来不会被修复.

解决方案是改变

@JoinFetch(JoinFetchType.OUTER)

@BatchFetch(BatchFetchType.JOIN)

这并不完全具有我希望的结果,最初希望生成的sql包含OUTER JOIN,
但BatchFetch只产生2个SQL,一个用于获取Table_A项,另一个用于获取所有Table_B项(包括其他标准要求)

转载注明原文:java – eclipselink在子类中忽略AdditionalCriteria - 代码日志