2017-09-20 48 views
3

如果我設置了具有附加標準約束的父級和子級關係的父/子關係,然後使用@JoinFetch,則childs additionalCriteria將被忽略。eclipselink在子類中被忽略的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個SQLS

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加載需求表-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] 

不添加從表B的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'); 
+0

我發現了錯誤; https://bugs.eclipse.org/bugs/show_bug.cgi?id=438316這是超大型的,沒有感動。任何解決方法? –

回答

0

因此,這是一個長期的錯誤與EclipseLink的和看起來並不像它會被固定。

解決的辦法是改變

@JoinFetch(JoinFetchType.OUTER) 

@BatchFetch(BatchFetchType.JOIN) 

這不正是有結果我所期待的,本來想生成的SQL包括一個OUTER JOIN, 但BatchFetch僅導致2個SQL,一個獲取Table_A項目,然後另一個獲取所有Table_B項目(包括附加標準要求)