I have three of many tables in Oracle (10g) database as listed below. I'm using Hibernate Tools 3.2.1.GA with Spring version 3.0.2.
- Product - parent table
- Colour - parent table
- ProductColour - join table - references
colourId
andprodId
ofColour
andProduct
tables respectively
Where the ProductColour
is a join table between Product
and Colour
. As the table names imply, there is a many-to-many relationship between Product
and ProductColour
. I think, the relationship in the database can easily be imagined and is clear with only this much information. Therefore, I'm not going to explore this relationship at length.
One entity (row) in Product
is associated with any number entities in Colour
and one entity (row) in Colour
can also be associated with any number of entities in Product
.
Let's say as for an example, I need to count the number of rows available in the Product
table (regarding Hibernate), it can be done something like the following.
Object rowCount = session.createCriteria(Product.class)
.setProjection(Projections.rowCount()).uniqueResult();
What if I need to count the number of rows available in the ProductColour
table? Since, it is a many-to-many relationship, it is mapped in the Product
and the Colour
entity classes (POJOs) with there respective java.util.Set
and no direct POJO class for the ProductColour
table is available. So the preceding row-counting statement doesn't seem to work in this scenario.
Is there a precise way to count the number of rows of such a join entity in Hibernate?