[ajug-members] JPA question
J. Talafous
jtalafous at gmail.com
Tue Nov 21 09:35:29 EST 2006
True. Everything is copacetic when the Map contains user-defined
classes that are declared in persistence.xml as an @Entity. Even
something like this line in a persisted entity:
@ManyToMany(cascade = CascadeType.ALL)
Map<String,Compound> compoundMap = new HashMap<String,Compound>();
is accepted by JPA when class Compound is an @Entity. But this line:
@ManyToMany(cascade = CascadeType.ALL)
Map<String, String> compoundMap = new HashMap<String, String>();
throws this exception during initialization (not during manager.persist() :
org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany
targeting an unmapped class:
gov.nih.ncgc.entities.Runset.plateMap[java.lang.String]
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:979)
(TopLink chokes too)
So it seems that simple Map of String->String can't be persisted in a
POJO? Say it ain't so. , Joe! The benefit of JPA is to not modify
any code in your POJO for persisting, just annotate it. I just found
this guy having the same question:
http://www.nabble.com/JPA-help-needed-for-maps-of-strings-t2516881.html
and they decided to build a workaround. We don't mind doing that if
we are using JPA properly but it is just an immature spec (I strongly
suspect not!). Has anyone out there been successful so we know a
solution exists or not? In the meanwhile, I will continue to
self-educate and research it... *thank you* for the help!!
On 11/21/06, Burr Sutter <burr.sutter at jboss.com> wrote:
> Typically Map and List are targeting other tables in the database.
>
> What should a list of strings map to? Most databases don't have array
> support the last time I checked.
>
>
>
>
> -----Original Message-----
> From: ajug-members-bounces at ajug.org
> [mailto:ajug-members-bounces at ajug.org] On Behalf Of J. Talafous
> Sent: Monday, November 20, 2006 3:41 PM
> To: General AJUG membership forum (100-200 messages/month)
> Subject: [ajug-members] JPA question
>
> I'm using Java Persistence API to avoid learning advanced SQL and
> doing any db design. So far it's working pretty well with Hibernate
> as the provider, but Toplink is working too, (but doesn't do per-table
> @Inheritance). For example, one of the fields in one of my classes:
>
> @ManyToMany
> Map<MyObject,AnotherObject> myMap = new
> HashMap<MyObject,AnotherObject>();
>
> persists nicely, no problem.
>
> BUT... anything I try to persist the following, FAILS:
>
> Map<String,String> myMap = new HashMap<String,String>();
>
> or this:
>
> List<String> myList = new ArrayList<String>()
>
> Both JPA providers are claiming that they don't see String.java as an
> entity class in persistence.xml, and since I don't have the code, I
> can't put it there.
>
> I could wrap String in another class, but this seems like not a good
> idea, because it seems JPA should be able to handle this. For
> example:
>
> String[] myList = new String[3]();
>
> gets persisted! Of course, all the @Basic String fields get persisted,
> so why is JPA having a problem with Strings inside a List or Map?
> it's so simple... I must be missing something here. Any ideas?
>
> Thanks!
> _______________________________________________
> ajug-members mailing list
> ajug-members at ajug.org
> http://www.ajug.org/mailman/listinfo/ajug-members
>
> _______________________________________________
> ajug-members mailing list
> ajug-members at ajug.org
> http://www.ajug.org/mailman/listinfo/ajug-members
>
More information about the ajug-members
mailing list