Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This should (?) work:</p> <pre><code>abstract class DAO[T, K &lt;: Serializable](implicit m: Manifest[T]) { ... def findByAttributes[T](attributes:AttributeValuePair[_]*):ArrayList[T] = { val cq = cb.createQuery(m.erasure) val queryRoot = cq.from(m.erasure) var criteria = cb.conjunction for (pair &lt;- attributes) criteria = cb.and(cb.equal(queryRoot.get(pair._1), pair._2 )) cq.where(Seq(criteria):_*) val results = em.createQuery(cq).getResultList results.asInstanceOf[ArrayList[T]] } } </code></pre> <p>[Edit]</p> <p>Aaargh!1!11!!!!</p> <p>I think you need to write <code>findByAttributes(...)</code>, not <code>findByAttributes[T](...)</code>, else that T shadows the T of the DAO class (which is the "right one"). I'm not sure that this solves your problem, but as it is, it is wrong.</p> <p>[Edit1]</p> <p>I didn't read the API careful enough. I think you want to use <a href="http://download.oracle.com/docs/cd/E17410_01/javaee/6/api/javax/persistence/criteria/Path.html#get%28javax.persistence.metamodel.SingularAttribute%29" rel="nofollow noreferrer">this Version of get</a>. </p> <p>So we would have to provide only the second type parameter of the SingularAttribute. The problem is that this would be the same as the one in AttributeValuePair[_]. I honestly don't know how to preceed here. You could try</p> <pre><code>def findByAttributes[A](attributes:AttributeValuePair[A]*):ArrayList[T] = {... </code></pre> <p>or</p> <pre><code>queryRoot.get[A](pair._1.asInstanceOf[SingularAttribute[T,A]]) </code></pre> <p>If this doesn't work, we get at least some interesting error messages, which may give us a hint :-)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload