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: Many-to-many: brings back too much data with Criteria API
PostPosted: Fri May 07, 2010 9:21 pm 
Newbie

Joined: Tue Oct 24, 2006 7:47 pm
Posts: 4
I have a many-to-many relationship that looks something like this in Hibernate/OO talk. Yes, many statements are linked to many accounts. It's a complex system!

Code:
Account <-> Statement

And:

Account account = getAccountFromSomewhere();
Set<Statement> statements = account.getStatements();

Statement statement = getStatementFromSomewhere();
Set<Account> accounts = statement.getAccounts();

And like this in DB talk:
Code:
acct <-> acct_stmt_map <-> stmt

I want to fetch all the statements where any one of its accounts has a status of "OPEN". Something like:
Code:
Criteria criteria = session.createCriteria(Statement.class);
criteria.createAlias("accounts", "a");
criteria.add(Restrictions.eq("a.status", "OPEN"));
List myList = criteria.list();

The problem here is that the generated SQL not only pulls in the statement -- but the associated account as well. Something like:
Code:
select s.col1, s.col2 ... , a.col1, a.col2, a.col3
from acct a, acct_stmt_map asm, stmt s
where s.statement_id = asm.statement_id and asm.account_id=a.account_id and a.status="OPEN"

I don't want the account data bringing back, nor do I want the object creating. Is there a way I can get Hibernate not to do this, so that it generates SQL something like this instead?
Code:
select s.col1, s.col2
from acct a, acct_stmt_map asm, stmt s
where s.statement_id = asm.statement_id and asm.account_id=a.account_id and a.status="OPEN"


Thanks
JC


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.