This page is no longer maintained — Please continue to the home page at www.scala-lang.org

JPA and @OneToMany mapping with Scala List

7 replies
Phil Miller
Joined: 2010-03-06,
User offline. Last seen 42 years 45 weeks ago.

Has anyone had any success using a @OneToMany JPA mapping in Scala,
using a Scala List, and not a java.util.List?

this works:
@OneToMany
var foos: java.util.List[Foo] = new java.util.ArrayList[Foo]

this doesn't:
@OneToMany
var foos: List[Foo] = Nil // error: Illegal attempt to map a non
collection as a @OneToMany

Implicit conversion between java & scala Lists is not helping as the
conversions seem to be out of scope. Using a ListBuffer does not work
either.

Any ideas folks?

Thanks for your time,
Phil.

Phil Miller 2
Joined: 2010-03-07,
User offline. Last seen 42 years 45 weeks ago.
JPA and @OneToMany mapping with Scala List

Has anyone had any success using a @OneToMany JPA (Hibernate) mapping
in Scala, using a Scala List, and not a java.util.List?

this works:
@OneToMany
var foos: java.util.List[Foo] = new java.util.ArrayList[Foo]

this doesn't:
@OneToMany
var foos: List[Foo] = Nil // error: Illegal attempt to map a non
collection as a @OneToMany

Implicit conversion between java & scala Lists is not helping as the
conversions seem to be out of scope. ListBuffer does not work either.

Any ideas folks?

Thanks for your time,
Phil.

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: JPA and @OneToMany mapping with Scala List

The JPA specification:

"2.1.1 Persistent Fields and Properties
...
Collection-valued persistent fields and properties must be defined in
terms of one of the following collection-valued interfaces regardless
of whether the entity class otherwise adheres to the JavaBeans method
conventions noted above and whether field or property-based access is
used:
java.util.Collection, java.util.Set, java.util.List, java.util.Map

For collection-valued persistent properties, type T must be one of
these collection interface types in the method signatures above.
Generic variants of these collection types may also be used(for
example, Set)."

This means that your JPA annotated collections cannot of type
scala.collection. Instead, you could use implicit conversions in other
places in your code where you use these collections.

As the JPA model is based on mutable collections, you have to be
careful using implicit conversions. You must only use conversions that
wrap the mutable java collection with a Scala interface, rather than
ones that copy the contents. It seems that
collection.JavaConversions._ will be okay, see below treating
java.util.ArrayList as a scala.collection.mutable.Buffer.

-jason

scala> val al = new java.util.ArrayList[String]()
al: java.util.ArrayList[String] = []

scala> al.add("0")
res20: Boolean = true

scala> import collection.JavaConversions._
import collection.JavaConversions._

scala> al(0) = "2"; al += "3"; al prepend "4"

scala> al
res21: java.util.ArrayList[String] = [4, 2, 3]

On Sun, Mar 7, 2010 at 8:43 AM, Phil Miller wrote:
> Has anyone had any success using a @OneToMany JPA (Hibernate) mapping
> in Scala, using a Scala List, and not a java.util.List?
>
> this works:
> @OneToMany
> var foos: java.util.List[Foo] = new java.util.ArrayList[Foo]
>
> this doesn't:
> @OneToMany
> var foos: List[Foo] = Nil // error: Illegal attempt to map a non
> collection as a @OneToMany
>
> Implicit conversion between java & scala Lists is not helping as the
> conversions seem to be out of scope.  ListBuffer does not work either.
>
> Any ideas folks?
>
> Thanks for your time,
> Phil.
>

bmaso
Joined: 2009-10-04,
User offline. Last seen 2 years 41 weeks ago.
Re: JPA and @OneToMany mapping with Scala List

And to add to Jason's info:

A JPA implementation introspects your JPA entity classes at runtime,
not compile time. At runtime of course the information about which
implicit conversions are available is no longer available. And in any
event, a Java-based implementation of a JPA wouldn't know anything
about Scala implicit conversions even if they were somehow buried in
the bytecode somewhere.

The JPA implementation needs to see one of j.u.{Collection, Set, List}
as the static type of the bean property in order to do correctly the
bytecode weaving or hidden object proxying that JPA entities do.

Best regards,
Brian Maso
(949) 395-8551
brian [at] blumenfeld-maso [dot] com
twitter: @bmaso
skype: brian.maso
LinkedIn: http://www.linkedin.com/in/brianmaso

On Sun, Mar 7, 2010 at 12:00 AM, Jason Zaugg wrote:
> The JPA specification:
>
> "2.1.1 Persistent Fields and Properties
> ...
> Collection-valued persistent fields and properties must be defined in
> terms of one of the following collection-valued interfaces regardless
> of whether the entity class otherwise adheres to the JavaBeans method
> conventions noted above and whether field or property-based access is
> used:
> java.util.Collection, java.util.Set, java.util.List, java.util.Map
>
> For collection-valued persistent properties, type T must be one of
> these collection interface types in the method signatures above.
> Generic variants of these collection types may also be used(for
> example, Set)."
>
> This means that your JPA annotated collections cannot of type
> scala.collection. Instead, you could use implicit conversions in other
> places in your code where you use these collections.
>
> As the JPA model is based on mutable collections, you have to be
> careful using implicit conversions. You must only use conversions that
> wrap the mutable java collection with a Scala interface, rather than
> ones that copy the contents. It seems that
> collection.JavaConversions._ will be okay, see below treating
> java.util.ArrayList as a scala.collection.mutable.Buffer.
>
> -jason
>
> scala> val al = new java.util.ArrayList[String]()
> al: java.util.ArrayList[String] = []
>
> scala> al.add("0")
> res20: Boolean = true
>
> scala> import collection.JavaConversions._
> import collection.JavaConversions._
>
> scala> al(0) = "2"; al += "3"; al prepend "4"
>
> scala> al
> res21: java.util.ArrayList[String] = [4, 2, 3]
>
>
> On Sun, Mar 7, 2010 at 8:43 AM, Phil Miller wrote:
>> Has anyone had any success using a @OneToMany JPA (Hibernate) mapping
>> in Scala, using a Scala List, and not a java.util.List?
>>
>> this works:
>> @OneToMany
>> var foos: java.util.List[Foo] = new java.util.ArrayList[Foo]
>>
>> this doesn't:
>> @OneToMany
>> var foos: List[Foo] = Nil // error: Illegal attempt to map a non
>> collection as a @OneToMany
>>
>> Implicit conversion between java & scala Lists is not helping as the
>> conversions seem to be out of scope.  ListBuffer does not work either.
>>
>> Any ideas folks?
>>
>> Thanks for your time,
>> Phil.
>>
>

Christopher Sch...
Joined: 2009-12-05,
User offline. Last seen 42 years 45 weeks ago.
Re: JPA and @OneToMany mapping with Scala List

I think its a good idea to use Java for the persistence data objects.

Regards Christopher

Am 07.03.2010 um 08:43 schrieb Phil Miller:

> Has anyone had any success using a @OneToMany JPA (Hibernate) mapping
> in Scala, using a Scala List, and not a java.util.List?
>
> this works:
> @OneToMany
> var foos: java.util.List[Foo] = new java.util.ArrayList[Foo]
>
> this doesn't:
> @OneToMany
> var foos: List[Foo] = Nil // error: Illegal attempt to map a non
> collection as a @OneToMany
>
> Implicit conversion between java & scala Lists is not helping as the
> conversions seem to be out of scope. ListBuffer does not work either.
>
> Any ideas folks?
>
> Thanks for your time,
> Phil.

Michael Fortin
Joined: 2010-02-16,
User offline. Last seen 42 years 45 weeks ago.
Re: JPA and @OneToMany mapping with Scala List

Has anyone tried porting OpenJPA or some other jpa api to scala? With nested annotation support in 2.8, would the only other hurdle be persistent collections?

Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: JPA and @OneToMany mapping with Scala List

similarily, i'm interested in JDO 2 for scala... trying out DataNucleus at the moment and was wondering if there is something more scala'ish, e.g. so that i don't need to dirty my code with java.util... collections and java.lang.... wrapper types.

ciao, -sciss-

Am 08.03.2010 um 15:44 schrieb Michael Fortin:

> Has anyone tried porting OpenJPA or some other jpa api to scala? With nested annotation support in 2.8, would the only other hurdle be persistent collections?
>

Christopher Sch...
Joined: 2009-12-05,
User offline. Last seen 42 years 45 weeks ago.
Re: JPA and @OneToMany mapping with Scala List

JPA is a Java specification... so a JPQL query will always expect to fill a java.util.Collection attribute (if you use OpenJPA or EclipseLink etc.)

Maybe it is sufficient to use implicit conversion and scala.collection's JavaConversions object while accessing the Java entities.

Nevertheless - I would really appreciate if someone has time to teach EclipseLink to use scala Collections? ;-)

Am 08.03.2010 um 16:44 schrieb Michael Fortin:

> Has anyone tried porting OpenJPA or some other jpa api to scala? With nested annotation support in 2.8, would the only other hurdle be persistent collections?
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland