tags:

views:

61

answers:

1

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 the 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>
A: 

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.

Thillakan
`where` works for read-only collections or when you're **really** dealing with a subset of data (and can therefore default missing columns on insert). It won't help with maintaining two separate collections based off the same table
ChssPly76