2013-01-01 110 views
0

我有兩個組的繼承表: 1.表A(由TableA1和TableA2繼承), 2.表B(由TableB1和TableB2繼承)休眠:Jointable加入特定子表

DiscriminatorColumn用於TableA的是'type',DiscriminatorValues分別是TableA1和TableA2的'A1'和'A2'。 類似地,TableB的DiscriminatorColumn是'type',DiscriminatorValues分別是TableB1和TableB2的'B1'和'B2'。 我有一個連接表TableA_B加入TableA & TableB。我知道連接表TableA_B只能將TableA1鏈接到TableB1,而將TableA2鏈接到TableB2,這種情況發生在'type'列中。即A1始終鏈接到類型B1,A2始終鏈接到類型B2。

現在,我的問題是,當我從TableA1執行查詢時,@JoinTable生成連接TableA1,TableA,TableB,TableB1和TableB2的sql。在這裏,從TableB2查詢是不需要的。有什麼辦法可以限制這個嗎?正如我所提到的,唯一的區別因素是「類型」欄。

TableA的實體:

@Entity 
@Table(name = "TableA") 
@Inheritance(strategy=InheritanceType.JOINED) 
@DiscriminatorColumn(name="type") 
public class TableA { 

    @Id 
    @GeneratedValue(strategy = IDENTITY) 
    @Column(unique = true, nullable = false) 
    private Long id; 
    private String name; 
    private String type; 

    @ManyToMany(fetch = FetchType.EAGER) 
     @JoinTable(
     name="TableA_B", 
     joinColumns={@JoinColumn(name="A_id")}, 
     inverseJoinColumns={@JoinColumn(name="B_id")} 
     ) 
    private Set<DataMaster> inputData = new HashSet<DataMaster>(0); 


//getters & setters ... 
} 

@Entity 
@Table(name = "TableA1") 
@PrimaryKeyJoinColumn(name="AId") 
@DiscriminatorValue("A1") 
public class TableA1 extends TableA{ 

    @Column(insertable=false, updatable=false) 
    private Long AId; 
    private String value1; 
//getters & setters ... 
} 

@Entity 
@Table(name = "TableA2") 
@PrimaryKeyJoinColumn(name="AId") 
@DiscriminatorValue("A2") 
public class TableA2 extends TableA{ 

    @Column(insertable=false, updatable=false) 
    private Long AId; 
    private String value2; 
//getters & setters ... 
} 

表B如下:

@Entity 
@Table(name = "TableB") 
@Inheritance(strategy=InheritanceType.JOINED) 
@DiscriminatorColumn(name="type") 
public class TableB { 

    @Id 
    @GeneratedValue(strategy = IDENTITY) 
    @Column(unique = true, nullable = false) 
    private Long id; 
    private String name; 
    private String type; 
//getters & setters ... 
} 

@Entity 
@Table(name = "TableB1") 
@PrimaryKeyJoinColumn(name="BId") 
@DiscriminatorValue("B1") 
public class TableB1 extends TableB{ 
    @Column(insertable=false, updatable=false) 
    private Long dataId; 
    private String value1; 
//getters & setters ... 
} 

@Entity 
@Table(name = "TableB2") 
@PrimaryKeyJoinColumn(name="BId") 
@DiscriminatorValue("B2") 
public class TableB2 extends TableB{ 
    @Column(insertable=false, updatable=false) 
    private Long dataId; 
    private String value2; 
//getters & setters ... 
} 

DAO:

public TableA1 find(Long id) { 
     Session session = DBSessionManager.getFactory().openSession(); 
     Transaction tx = null; 
     TableA1 tableA1 = null; 
     try{ 
     tx = session.beginTransaction(); 
     tableA1 = (TableA1)session.get(TableA1.class, id); 
     tx.commit(); 
     }catch (HibernateException e) { 
     if (tx!=null) tx.rollback(); 
     }finally { 
     session.close(); 
     }  
     return tableA1; 
} 

我嘗試使用FilterJoinTable爲:

@FilterJoinTable(name="dataTypeFilter", condition=":dataType = type") 

而在DAO,

session.enableFilter("dataTypeFilter").setParameter("dataType", "B1"); 

而且,這也沒有給預期的結果。

這是我如何添加過濾器:

@Entity 
@Table(name = "TableA") 
@Inheritance(strategy=InheritanceType.JOINED) 
@DiscriminatorColumn(name="type") 
@FilterDef(name="dataTypeFilter", parameters = { 
     @ParamDef(name = "dataType", type = "string") 
     }) 
public class TableA { 

    @Id 
    @GeneratedValue(strategy = IDENTITY) 
    @Column(unique = true, nullable = false) 
    private Long id; 
    private String name; 
    private String type; 

    @ManyToMany(fetch = FetchType.EAGER) 
     @JoinTable(
     name="TableA_B", 
     joinColumns={@JoinColumn(name="A_id")}, 
     inverseJoinColumns={@JoinColumn(name="B_id")} 
     ) 
    @FilterJoinTable(name="dataTypeFilter", condition=":dataType = type") 
    private Set<DataMaster> inputData = new HashSet<DataMaster>(0); 


//getters & setters ... 
} 

DAO:

public TableA1 find(Long id) { 
      Session session = DBSessionManager.getFactory().openSession(); 
      Transaction tx = null; 
      TableA1 tableA1 = null; 
      try{ 
      tx = session.beginTransaction(); 
     session.enableFilter("dataTypeFilter").setParameter("dataType", "B1"); 
      tableA1 = (TableA1)session.get(TableA1.class, id); 
      tx.commit(); 
      }catch (HibernateException e) { 
      if (tx!=null) tx.rollback(); 
      }finally { 
      session.close(); 
      }  
      return tableA1; 
    } 
+0

你可以用'@ FilterDef'和'@ FilterJoinTable'顯示代碼 - 很難確切地知道發生了什麼,但沒有看到@ FilterDef' –

+0

我用過濾器代碼更新了問題。謝謝。 –

回答

0

從你的描述,你應該有TableA1和TableB1和TableA2和TableB2之間的另一種多對多的關聯之間的多對多的關聯。但是您將這兩個關聯映射爲TableA和TableB之間的單個關聯。您應該將這兩個關聯映射爲他們真正的地址:

private class TableA1 extends TableA { 
    @ManyToMany 
    private Set<TableB1> tablesB1s; 
} 

private class TableA2 extends TableA { 
    @ManyToMany 
    private Set<TableB2> tablesB2s; 
} 

我不會使用相同的連接表映射兩個不同的關聯。我甚至不確定這是可能的。