I am currently trying to build an SQL (plain SQL, directly executed on the database - I'm not using hibernate for this in particular) to get data from my database. It's an export based on a Java-implemented (hibernate) criteria query to which I want to add additional information from additional tables.
In Java Criteria queries, I have a statement as follows:
Criteria criteria = session.createCriteria(MyEntity.class);
criteria.createCriteria("fk").add(Restrictions.in("sysid", <<someListofIDsfromPreviousQuery>>))
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
return criteria.list();
Which results in a 1-1 result set from a list of IDs from Table A ("someListofIDsFromPreviousQuery") to which there are multiple results in Table B ("MyEntity.class").
I know what DISTINCT_ROOT_ENTITY does but I'm wondering how it actually works, meaning, what would an equivalent SQL look like?
Example according to the criteria query above:
SELECT COUNT(*) FROM TABLEA a
WHERE a.property1=someValue AND a.property2=someOtherValue;
Returns 2000 results. (This is my list of IDs)
SELECT COUNT(*) FROM TABLEA a
JOIN TABLEB b ON b.fk = a.sysid -- <-- this is basically equivalent to what the criteria query above does, no?
WHERE a.property1=someValue AND a.property2=someOtherValue;
Returns 2500 results.
How does Critera.DISTINCT_ROOT_ENTITY actually chose which entry of TABLEB to pick as its "distinct entity"? Or am I getting something wrong here?
If it makes a difference: I'm trying to write my query in OracleDB SQL Dialect.
After some searching, experimenting, and generally being confused - it seems as if DISTINCT_ROOT_ENTITY does not make any actual decision making in how it determines the values from Table B. It just takes the first value of the result set from the JOIN "randomly".
So the code I'm trying to base my SQL off of is actually wrong and dangerous for the application it's in. Good to know. Cough