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