I have begun writing a webserver using Scala+Akka, combining it with Hibernate+MySQL for data storage. Despite spending several days translating Java+Hibernate tutorials over to Scala, I got something working.
Currently, I can successfully:
Get an entity by it's ID field specifically, using:
session.get(classOf[T], id)
Get a list of entries using:
val builder: CriteriaBuilder = sessionFactory.getCriteriaBuilder
val query: CriteriaQuery[T] = builder.createQuery(classOf[T])
val root: Root[T] = query.from(classOf[T])
val all: CriteriaQuery[T] = query.select(root)
val allQuery: TypedQuery[T] = session.createQuery(all)
... (convert results to Seq[T]
Next, I wanted to implement getting a list of entries with a specific column value. From what I read online, it should be the following:
val cb = sessionFactory.getCriteriaBuilder
val query = cb.createQuery(classOf[T])
val root = query.from(classOf[T])
val queryResults: TypedQuery[T] =
session.createQuery(query.select(root).where(root.get("status").equalTo(status)))
The problem here is that it seems query.where
result is both a Predicate*
and a Expression[Boolean]
, which causes this error:
I managed to put this error off for querying by ID by using the session.get
function, but it seems I will need to fix this to query by specific fields, unless there is an alternative approach to querying?
Any suggestions?
Ok so the issue is with the fact that the Predicate returned by equalsTo
is also of type Expression[Boolean]
, so the exact function to call is ambiguous.
I fixed the issue with another Scala -> Java hack...
val cb = sessionFactory.getCriteriaBuilder
val query = cb.createQuery(classOf[T])
val root = query.from(classOf[T])
val expression: Predicate = root.get("status").equalTo(status) // <= Create predicate
val predicates: Array[Predicate] = Array(expression) // <= Store predicate in Scala Array
// Call where, getting the Java array from the Scala Array
val all = query.select(root).where(predicates:_*)
val queryResults: TypedQuery[T] = session.createQuery(all)
By passing the Java array of the Predicates, it is then not ambiguous and the function where(Predicate... predicates)
is called.