我有两组继承表: 1. TableA(由TableA1和TableA2继承), 2. TableB(由TableB1和TableB2继承)
TableA的DiscriminatorColumn为'type',DiscriminatorValues分别为TableA1和TableA2的'A1'和'A2'。 类似地,TableB的DiscriminatorColumn是'type',DiscriminatorValues分别是TableB1和TableB2的'B1'和'B2'。 我有一个连接表TableA_B用于加入TableA&表B。我知道连接表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 :(得分:0)
根据您的描述,您应该在TableA1和TableB1之间建立ManyToMany关联,并在TableA2和TableB2之间建立另一个ManyToMany关联。但是您将这两个关联映射为TableA和TableB之间的单个关联。您应该将这两个关联映射为它们的真实含义:
private class TableA1 extends TableA {
@ManyToMany
private Set<TableB1> tablesB1s;
}
private class TableA2 extends TableA {
@ManyToMany
private Set<TableB2> tablesB2s;
}
我不会使用相同的连接表来映射两个不同的关联。我甚至不确定它是否可能。