-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Criteria Search on Bi-directional Association
PostPosted: Sat Jan 07, 2012 1:53 am 
Newbie

Joined: Wed Jul 27, 2011 9:29 pm
Posts: 4
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.