I'm using Scala to do typesafe JPA2 Criteria Queries. Therefore i have a Java MetaModel Class (the only Java in my code, the rest is Scala -> pure Scala problem), which holds my model attributes:
@StaticMetamodel(User.class)
public class User_ {
public static volatile SingularAttribute<User, Long> id;
public static volatile SingularAttribute<User, String> name;
}
To do a query for one single attribute, i have this function:
def findByAttribute[T](
attribute:SingularAttribute[User, T], value:T):ArrayList[User] =
{
...
}
Which i can call like this:
userEJB.findByAttribute(User_.name, "John")
Now i'm trying to create a query function, with which i can query for multiple attributes at once, and therefore i want to use a Map of SingularAttributes as a parameter for my function:
// Map is of type scala.collection.immutable.Map
def findByAttributes[T](
attributes:Map[SingularAttribute[User, T], T]):ArrayList[User] =
{
...
}
Ok, so the function should work... But how can i call it??? Say for example i want to query with a Map like this:
User_.name -> "Doe"
User_.id -> 5
So my first approach of defining this Map in Scala and passing it to findByAttributes is this:
val criteria = Map(User_.name -> "Doe", User_.id -> 5)
// Produces Compiler Error
val users = userEJB.findByAttributes(criteria)
Unfortunately, the compiler isn't satisfied when passing searchFor to the findByAttributes-function, producing the error below:
no type parameters for method findByAttributes: (attributes:
Map[javax.persistence.metamodel.SingularAttribute[net.teachernews.model.User,
T],T])
java.util.ArrayList[net.teachernews.model.User] exist so that it can be applied to
arguments (scala.collection.immutable.Map[javax.persistence.metamodel.
SingularAttribute[
net.teachernews.model.User, _
>: java.lang.Long with java.lang.String
<: java.lang.Comparable[_
>: java.lang.Long with java.lang.String
<: java.lang.Comparable[_ >: java.lang.Long with java.lang.String
<: java.io.Serializable] with java.io.Serializable]
with java.io.Serializable],Any]) --- because ---
argument expression's type is not compatible with formal parameter type;
found :
scala.collection.immutable.Map[javax.persistence.metamodel.SingularAttribute[
net.teachernews.model.User, _
>: java.lang.Long with java.lang.String
<: java.lang.Comparable[_
>: java.lang.Long with java.lang.String
<: java.lang.Comparable[_ >: java.lang.Long with java.lang.String
<: java.io.Serializable] with java.io.Serializable] with java.io.Serializable],
Any]
required: Map[javax.persistence.metamodel.SingularAttribute[
net.teachernews.model.User,?T],?T]
That's the most complicated generic problem i ever had. A bit too high for my skill ;) Anyone knows how i can build the right map type that i can pass to the function? Is it even possible, or can't the compiler infer the type anymore in my case? Or am i using the wrong datastructure?