我有两组继承表:1.TableA(由TableA1和TableA2继承),2.TableB(由TableB1和TableB2继承)
TableA 的 DiscriminatorColumn 是“type”,TableA1 和 TableA2 的 DiscriminatorValues 分别是“A1”和“A2”。同样,TableB 的 DiscriminatorColumn 是“type”,TableB1 和 TableB2 的 DiscriminatorValues 分别是“B1”和“B2”。我有一个用于连接 TableA 和 TableB 的连接表 TableA_B。我知道连接表 TableA_B 只能将 TableA1 链接到 TableB1,而 TableA2 只能链接到 TableB2,这根据“类型”列发生。即,A1 总是链接到类型 B1,而 A2 总是链接到类型 B2。
现在,我的问题是,当我从 TableA1 进行查询时,@JoinTable 会生成连接 TableA1、TableA、TableB、TableB1 和 TableB2 的 sql。在这里,不需要从 TableB2 进行查询。有没有办法限制这个?正如我所提到的,唯一的区别因素是“类型”列。
表A实体:
@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 ...
}
道:
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 ...
}
道:
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;
}