How to split a table's data into two sets based on a column value with Hibnernate mappings?
Suppose there are three tables:
Table A (ID, NAME)
Table B (ID, NAME)
Table A-B (A_ID, B_ID, Boolean_Property)
and Table A and B are modeled by classes A and B as follows:
public class A {
private long id;
private String name;
// All the B's that belong to A where the boolean property is True
Set<B> trueBSet;
// All t开发者_运维百科he B's that belong to A where the boolean property is False
Set<B> falseBSet;
}
public class B {
private long id;
private String name;
}
How would I model this using Hibernate? I would like to be able to do the following, but it appears that discriminator column values don't exist for set attributes:
<class name="A" table="A">
<id name="id" column="ID">
<generator class="native" />
</id>
<property name="name" column="NAME" />
<set name="trueB" table="A-B">
<key column="A_ID"/>
<many-to-many column="B_ID" class="B"/>
<discriminator column="Boolean_Property" value="True" />
</set>
<set name="falseB" table="A-B">
<key column="A_ID"/>
<many-to-many column="B_ID" class="B"/>
<discriminator column="Boolean_Property" value="False" />
</set>
</class>
I think, you can apply where clause in set tag. Hibernate document state as follows in following link http://docs.jboss.org/hibernate/core/3.3/reference/en/html/collections.html
where (optional): specifies an arbitrary SQL WHERE condition that is used when retrieving or removing the collection. This is useful if the collection needs to contain only a subset of the available data.
as @ChssPly76 said this is a duplicate of Multiple @ManyToMany sets from one join table
The solutions there suggest to use a view as Join-Column and plain SQL for CRUD operations.
This Answer is marked community wiki so if anyone else wants this question to leave the 'unanswered' section may freely upvote it.
精彩评论