Hello there,
I am struggling in finding a way to get a list of objects with bi-directional association.
For example, I have object A and B and they are associated bi-directionally.
In object A, I have method like: @OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = "B") public List<B> getB() { return b; }
In object B, I have method like:
@ManyToOne @JoinColumn(name = "AID") public A getA() { return a; }
Table B has following column: ID - the auto incremented primary key AID - the foreign key referenced to table A other fields.
Now I would like to find all the rows in table B whose AID is in a list. Here is what the code should look like:
List<Integer> list = some list populated. Criteria crit = session.createCriteria(B.class); Criterion criterion = Restrictions.in("aid", list); crit.add(criterion) .addOrder(order) .setFirstResult(startIndex) .setMaxResults(limit);
List<B> bList = (List<B>)crit.list();
The problem is above code does not work since there is not setAID() and getAID() method in object B. And the AID is set when call setA() method and AID is gotten by using getA.getId() method.
Just wonder is there any solution so I can get a list of B object? (I don't want to use SQL)
Highly appreciated for any hint.
|