Problem with QueryDSL

37 views
Skip to first unread message

Mikko Tanskanen

unread,
Oct 29, 2020, 6:30:12 AM10/29/20
to ScalikeJDBC Users Group
Hi, 

I'm trying to execute the following query: 

withSQL(select(i.id)
  .from(Image as i)
  .where.exists(
    select.from(PurchasedImage as pi)
      .leftJoin(Purchase as p)
        .on(pi.purchaseId, p.id)
      .where.eq(pi.imageId, i.id)
     .and
     .eq(p.userId, userId)
      .and
      .ge(p.validTo, ZonedDateTime.now())
    ))
    .map(Image(i.resultName))
    .list().apply()

But I get: 
Execution exception[[ResultSetExtractorException: Failed to retrieve value because The column name i_on_i was not found in this ResultSet.. If you're using SQLInterpolation, you may mistake u.id for u.resultName.id.]]

At the end, I would like to have the exist result as true/false value, like in this sql: 
select i.d, exists (
select pi.id 
from purchased_image pi 
left join purchase p on p.id = pi.purchase_id
where pi.image_id = i.id and p.valid_to > NOW() and p.user_id = 18) as hasAccess
from image i

But I'm at lost how do I perform this query in scalikejdbc. 

Thanks in advance

Kazuhiro Sera

unread,
Oct 29, 2020, 6:34:26 AM10/29/20
to Mikko Tanskanen, ScalikeJDBC Users Group
select(i.result.id) should work for you

--
You received this message because you are subscribed to the Google Groups "ScalikeJDBC Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalikejdbc-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalikejdbc-users-group/949a7025-d6cf-4c05-af44-295eec50fcc2n%40googlegroups.com.

Mikko Tanskanen

unread,
Oct 29, 2020, 6:41:39 AM10/29/20
to ScalikeJDBC Users Group
I tried that, but that gives me: 
 Execution exception[[ResultSetExtractorException: Failed to retrieve value because The column name dfi_on_i was not found in this ResultSet.. If you're using SQLInterpolation, you may mistake u.id for u.resultName.id.]]

Kazuhiro Sera

unread,
Oct 29, 2020, 6:49:00 AM10/29/20
to Mikko Tanskanen, ScalikeJDBC Users Group
dfi_on_i is not the id column. Probably, is it dxx_fxxx_id?

Mikko Tanskanen

unread,
Oct 29, 2020, 6:57:07 AM10/29/20
to ScalikeJDBC Users Group
So now I have: 
withSQL(select(i.result.id)

  .from(Image as i)
  .where.exists(
    select.from(PurchasedImage as pi)
      .leftJoin(Purchase as p)
        .on(pi.purchaseId, p.id)
      .where.eq(pi.imageId, i.id)
     .and
     .eq(p.userId, userId)
      .and
      .ge(p.validTo, ZonedDateTime.now())
    ))
    .map(Image(i.resultName))
    .list().apply()  

And I get that dfi_on_i was not found...

These error messages are pretty cryptic, I don't know how to approach this problem. 

Kazuhiro Sera

unread,
Oct 29, 2020, 7:21:05 AM10/29/20
to Mikko Tanskanen, ScalikeJDBC Users Group
What does Image() method do internally? I guess the method accesses the field dxxFxxId. Also, I recommend enabling query logs http://scalikejdbc.org/documentation/query-inspector.html


Mikko Tanskanen

unread,
Oct 29, 2020, 7:29:12 AM10/29/20
to Kazuhiro Sera, ScalikeJDBC Users Group
That query is implemented in Image object, which is generated by scalikejdbcGen. 
Image is a case class, also generated by scalikejdbcGen.

If I "go to implementation" of Image method in the map funcion, it suggests this generated method: 
def apply(i: ResultName[Image])(rs: WrappedResultSet): Image = 
  new Image(
    id = rs.get(i.id), 
    createdAt = rs.get(i.createdAt)...)

What is dxxFxxId field? 

Kazuhiro Sera

unread,
Oct 29, 2020, 7:38:43 AM10/29/20
to Mikko Tanskanen, ScalikeJDBC Users Group
I don't know what the exact column name is but the table should have dxxxx_fxxxxx_ixxxx column (e.g., device_firmware_id). And the apply method should have corresponding deviceFirmwareId = rs.get(i.deviceFirmwareId) or something like that.

If you're not intentionally using select(i.result.id).from, select.from should work for you.

Mikko Tanskanen

unread,
Oct 29, 2020, 8:44:45 AM10/29/20
to ScalikeJDBC Users Group
Ah, I get it, thanks!

Now that I have that sorted out, I would need the result of 
exists (select pi.id 
  from purchased_image pi 
  left join purchase p on p.id = pi.purchase_id
  where pi.image_id = i.id and p.valid_to > NOW() and p.user_id = ${userId}) as hasAccess

to be a part of the result (see the first post in this thread). Can this be done with QueryDSL? I have now: 

withSQL(select(
  i.result.id,
  i..., (other fields from image)
  select(pi.id).from(PurchasedImage as pi)

    .leftJoin(Purchase as p)
      .on(pi.purchaseId, p.id)
    .where.eq(pi.imageId, i.result.id)

    .and
    .eq(p.userId, userId)
    .and
    .ge(p.validTo, ZonedDateTime.now()))
  .from(Image as i))
  .map(Image(i.resultName))
.  list().apply()

But I get "required SQLSyntax, found ConditionSQLBuilder[Nothing]"
I added hasAccess to Image case class property and apply method (there isn't such column in the table).

Can this be done with queryDSL or should it be done some other way? 
Reply all
Reply to author
Forward
0 new messages