4

As described here

If you want to make the code compile you have to add the :_* keyword to the condition:Predicate

Now I have this problem

val queryBuilder = em.getCriteriaBuilder()
     val cq = queryBuilder.createQuery(classOf[Product])
     val product:Root[Product] = cq.from(classOf[Product])
     val condition:Predicate = queryBuilder.equal(product.get("name"), "name")
     --> cq.where(condition:_*)


Multiple markers at this line
- type mismatch; found : javax.persistence.criteria.Predicate required: Seq[?]

Any idea?

3 Answers 3

8

You must not use :_* here.

There reason why _* exists is that varargs may lead to ambiguities. In java If you have f(int... values) you may call f(1, 2, 3), but also have do

int[] values = {1, 2, 3}; 
f(values)

With ints, it is ok. But if you have f(object.. values) and have object[] values = {"a", "b"}, when calling f(values), should it means that f is called with a single arg, which is the values, or with multiple args "a" and "b"? (java chooses the later).

To avoid that, in scala, when there are varargs, it is not allowed to pass them as arrays (actually the more general Seq in scala) argument, except if you explicitly says you are doing so, by putting the abscription :_*. With the previous example, f(values) means values is the single argument. f(values: _*) means each element of values, whether there are zero, one, or many, is an argument.

In your particular case, you are passing the Predicate arguments (actually just one) as separate (well...) predicates, not as a collection of predicates. So no :_*

Edit: I read the post you linked to more carefully. While what I certainly believe what I have written above is true, it is probably not helpful. The proper answer was given by MxFr:

  1. Since scala 2.9, interaction with java varargs is ok. just pass condition
  2. Before that, you must pass your arguments as an array, and put :_*
Sign up to request clarification or add additional context in comments.

Comments

4

With Scala 2.9 cq.where(condition) should work, even cq.where(condition1, condition2) should work.

Alternatively you could use cq.where(Array(condition):_*).

1 Comment

cq.where(Array(condition):_*) is what made it work for me in Scala 2.13
0

I'm using scala library 2.9.0.1. This is the compiler error for that code, only passing the condition:

[ERROR]     D:\Mazi\Develop\workspaces\scala\sshwWebApp\src\main\scala\com\example\app\dao\DefaultProductDao.scala:54: error: ambiguous referenc
    e to overloaded definition,
    [INFO] both method where in trait CriteriaQuery of type (x$1: <repeated...>[javax.persistence.criteria.Predicate])javax.persistence.criteria
    .CriteriaQuery[com.example.app.domain.Product]
    [INFO] and  method where in trait CriteriaQuery of type (x$1: javax.persistence.criteria.Expression[java.lang.Boolean])javax.persistence.cri
    teria.CriteriaQuery[com.example.app.domain.Product]
    [INFO] match argument types (javax.persistence.criteria.Predicate)
    [INFO]      cq.where(condition)
    [INFO]         ^

Where is the problem?

If I use

cq.where(Array(condition):_*)

I receive the same compiler error:

[ERROR] D:\Mazi\Develop\workspaces\scala\sshwWebApp\src\main\scala\com\example\app\dao\DefaultProductDao.scala:54: error: ambiguous referenc
e to overloaded definition,
[INFO] both method where in trait CriteriaQuery of type (x$1: <repeated...>[javax.persistence.criteria.Predicate])javax.persistence.criteria
.CriteriaQuery[com.example.app.domain.Product]
[INFO] and  method where in trait CriteriaQuery of type (x$1: javax.persistence.criteria.Expression[java.lang.Boolean])javax.persistence.cri
teria.CriteriaQuery[com.example.app.domain.Product]
[INFO] match argument types (javax.persistence.criteria.Predicate)
[INFO]      cq.where(Array(condition):_*)
[INFO]         ^

Maybe the scala compiler that I use?

KInd regards Massimo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.