1

This is the SQL statement that I have.

SELECT USER_PROFILE.FIRST_NAME, USER_PROFILE.LAST_NAME, USER_PROFILE.USER_TYPE
FROM USER_PROFILE
INNER JOIN USER_LOGIN_STATUS
ON USER_PROFILE.USER_ID=USER_LOGIN_STATUS.USER_ID
ORDER BY USER_PROFILE.FIRST_NAME

And I'm trying to execute the code below that I thought the equivalent to hibernate DetachedCriteria and expected to only have two data as a result.

DetachedCriteria dc = getDetachedCriteria();
DetachedCriteria userLoginCriteria = DetachedCriteria.forClass(UserLoginStatus.class);
userLoginCriteria.setProjection(Projections.distinct(Projections.property("userId")));
dc.add(Subqueries.propertyIn(UserField.id.name(), userLoginCriteria));

DetachedCriteria profileCriteria = dc.createCriteria("profile");
profileCriteria.addOrder(Order.asc("firstName"));
return getAll(dc, pageSetting);

But unfortunately this is the unexpected result: I am having a multiple data result.

Name | Type |

  1. Ben Jones | User |
  2. Ben Jones | User |
  3. Tom Homer | Guest |
  4. Tom Homer | Guest |

Is anyone there knows the exact equivalent DetachedCriteria or a solution for this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
kwan_ah
  • 1,061
  • 1
  • 12
  • 18
  • Why are you doing a sub-query in the detached criteria when in the SQL you provided you are not? – Tony Day Jan 08 '13 at 09:10
  • oh I'm so sorry. I am new to this technology. I really don't know how to get it work in detached criteria. Can you help me figure out how to get it done in detached criteria using the sql I provided.Many thanks. – kwan_ah Jan 09 '13 at 04:11

2 Answers2

1

First of all, your SQL looks incorrect. The reason it is returning multiple rows is because you're joining against the USER_LOGIN_STATUS table which may have multiple rows per USER_PROFILE. Because you are not selecting any fields from the USER_LOGIN_STATUS table, you cannot see why there are multiple rows. Why are you joining on this table in the first place?

Secondly, the detached criteria you are performing is not equivalent to the SQL you have provided since you are doing a sub-query which you are not in the SQL.

You don't need this sub-select and since I don't understand why you are doing the join I will assume some points to give you the following example:

DetachedCriteria dc = getDetachedCriteria();
dc.createAlias("userLoginStatus", "uls");
dc.add(Projections.property("firstName")); 
dc.add(Projections.property("lastName"));
dc.add(Projections.property("userType")); 
dc.addOrder(Order.asc("firstName")); 
return getAll(dc, pageSetting);

This is now roughly equivalent but I am assuming:

  • You have the correct mappings for your relationship between UserField and UserLoginStatus.
  • That getDetachedCriteria() is effectively returning DetachedCriteria.forClass(UserField.class).

You can also now refer to a field in UserLoginStatus as so:

dc.add(Projections.property("uls.my_user_login_field"));

And as well, if you get your query sorted out and you still return multiple entities, then dinukadev's answer will then come into play with:

dc.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

I suspect the reason this isn't working for you is because of your sub-select.

Sorry I cannot help you more.

Tony Day
  • 2,170
  • 19
  • 25
  • Thank you so much for being so nice. I really appreciate it. I'm pretty sure this one will work. :) – kwan_ah Jan 09 '13 at 08:41
0

Please try to set the result transformer on your root detached criteria as follows. This will eliminate duplicates.

dc.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);  
dinukadev
  • 2,279
  • 17
  • 22
  • I already tried your code but it doesn't work. Thanks for helping. – kwan_ah Jan 07 '13 at 00:40
  • Thank you for your help. I am searching the question in here and then I try your code. It works! I have been disturbed by this question for half a day. I don't know it can not help Mr.kwan_ah but it works to mine. Thank you! – MageXellos Feb 07 '15 at 06:04
  • Glad it helped you out :) – dinukadev Feb 08 '15 at 16:30