views:

235

answers:

0

I have two entities, let's call them X and Y. They have many-to-many relation but I never needed to use that relation, i.e x.getYs(), so I did not set it up to keep things simple. I have a table that holds the relationship and has only rows that are keys to X and Y. In other words that x_y table's rows are only x_id and y_id

What I needed to do is to work on the relations itself. So, I created an entity XY. Since x_y has no primary key I had to create a XYPk class that behaves as a PK as described here.

@Embeddable
class XYPk implements Serializable
{
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "x_id")
    private X x;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "y_id")    
    private Y y;
}

My problem is when I do session.createCriteria(XY.class).list(); incorrect results are returned, there are duplicate records, missing records. The number of records returned is correct thou.

When I change XYPk's fetching types to LAZY the problem disappears.

Is this a bug or I am doing something wrong?