我无法创建可以给我适当答复的好的标准。这就是我的数据库的样子:
Table A : this is a table that one column has references to Table B.
Table B : this is a table that contains two subtable b1 and b2.
Table b1: this is a table that one column has references to Table C.
Table b2: this is a table that one column has references to Table D.
Table C : this is a destination1.
Table D : this is a destination2.
为了更好地理解,这是A.hbm.xml文件:
<class name="A" table="A" schema="database">
<many-to-one name="B" column="b_id" class="package.B"/>
</class>
和B.hbm.xml文件:
<class name="B" table="B" schema="database">
<subclass name="B1" discriminator-value="b1" >
<many-to-one name="b1" column="b1_id" class="package.B1"/>
</subclass>
<subclass name="B2" discriminator-value="b2" >
<many-to-one name="b2" column="b2_id" class="package.B2"/>
</subclass>
</class>
我想从表A中获得一个列表,但是由于A中的某些数据引用到b1,另一些引用到b2,criteria.createAlias()将返回0数据。我也有技巧来制定两个单独的标准并将结果合并在一起,但这是一种正确的方法,有时会因错误而告终。 这是我的代码
private Criteria completeCriteria(Criteria c) {
c.createAlias(A.PROP_B, "B");
// alias from first subtable
c.createAlias("B."+b1.PROP_C, "C");
System.out.println("1# : "+c.list().size)
// alias from second subtable
c.createAlias("B."+b2.PROP_D,"D");
System.out.println("2# : "+c.list().size)
....
}
输出类似于:
1# : 10
2# : 0
注意1:PROP_D是引用表D的列的名称
注意2:我写的东西对这个问题很重要!有些东西不在这里。