Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EnsureIndexes() fails when my Entity contains Embedded List<HashMap<K, V>> #923

Closed
fedjo opened this issue Apr 19, 2016 · 11 comments
Closed
Labels
Milestone

Comments

@fedjo
Copy link

fedjo commented Apr 19, 2016

Hello,

I have a class (ket's say MyClass.java) annotated with @Entity with has a field of type List<HashMap<K, V>> field_1 annotated with @Embedded.

I can store documents in a proper way as list of DBObjects eg:

[{ .... }, { ... }, {},... ]

but when I'm running Datastore's method
ensureIndexes(MyClass.class) I get the following exception,

java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast to java.lang.Class at org.mongodb.morphia.mapping.MappedField.discoverType(MappedField.java:199) ~[morphia-1.0.1.jar:na] at org.mongodb.morphia.mapping.MappedField.discover(MappedField.java:117) ~[morphia-1.0.1.jar:na] at org.mongodb.morphia.mapping.MappedField.<init>(MappedField.java:95) ~[morphia-1.0.1.jar:na] at org.mongodb.morphia.mapping.MappedClass.discover(MappedClass.java:214) ~[morphia-1.0.1.jar:na] at org.mongodb.morphia.mapping.MappedClass.<init>(MappedClass.java:145) ~[morphia-1.0.1.jar:na]

As you can understand I use morphia version 1.0.1.
I have tried with 1.1.1 having the same exception, and with 0.111 I couldn't compile my code!

@evanchooly
Copy link
Member

can you post your entity source?

@fedjo
Copy link
Author

fedjo commented May 25, 2016

The indexes annotations are

@Indexes({
        @Index(fields = @Field(value = "resourceType", type = IndexType.ASC), options = @IndexOptions()),
        @Index(fields = @Field(value = "administrative.withCreator", type = IndexType.ASC), options = @IndexOptions()),\
        @Index(fields = @Field(value = "administrative.externalId", type = IndexType.ASC), options = @IndexOptions()),
        @Index(fields = @Field(value = "provenance.provider", type = IndexType.ASC), options = @IndexOptions()),
        @Index(fields = @Field(value = "provenance.resourceId", type = IndexType.ASC), options = @IndexOptions()),
        @Index(fields = @Field(value = "descriptiveData.label", type = IndexType.ASC), options = @IndexOptions()),
        @Index(fields = @Field(value = "collectedIn", type = IndexType.ASC), options = @IndexOptions()) })

and the field which creates the problem is

@Embedded
private List<HashMap<MediaVersion, EmbeddedMediaObject>> media;

Because the full entity was to big I pasted it on that link

[http://pastebin.com/KNUa1VcR]

thanks again,
@evanchooly

@evanchooly
Copy link
Member

Great, thanks. I'll try to play with this today and see what shakes out.

@evanchooly
Copy link
Member

I had to massage the code a little to get things to compile/resolve properly (because I don't have your entire model which is fine-ish) and i get a different error. "Generic TypeVariable not supported" which is more in keeping with what I expected to see since Morphia has issues mapping unbound type parameters. I don't suppose you can put together a complete test case and zip it up somewhere?

@fedjo
Copy link
Author

fedjo commented May 27, 2016

Sorry for being late :-P
Compile it and run it should give the following error:

Exception in thread "main" java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast to java.lang.Class

morphiaBug.zip

@evanchooly evanchooly added this to the 2.0 milestone May 31, 2016
@evanchooly
Copy link
Member

Looking in to this, I see where the problem is and even have a good grasp on what the fix is. Sadly, it duplicates a lot of the work targeted for 2.0(ish) which includes a rewrite of the mapping code. I'm not quite ready, yet, to say "just wait for 2.0" because that's not a great answer but I'm mulling my options here. I'd rather not go through the work of (re)fixing this code only to throw it away in a (hopefully) few months.

@fedjo
Copy link
Author

fedjo commented Jun 1, 2016

I totally agree @evanchooly .

Otherway or not we have managed to overcome this problem by indexing the field we want manually.
So I will wait for version 2.0.

PS: Could you give me a hint about the bug or the code in order to try to understand how the mapping system of Morphia works?
I would really like to patch morphia by myself!

@evanchooly
Copy link
Member

It has to do with how morphia is discovering and tracking generic types. The code is rather gnarly which is why it's being replaced. MappedField and EphemeralMappedField (terrible name even though I chose it) are the guts of where the failure lies.

@cmoetzing
Copy link

cmoetzing commented Jun 6, 2016

"just wait for 2.0"

For me that is fine. The described workaround from #921 is just a few lines long and therefore does not hurt. I'm happy to remove it when 2.0 arrives. ;-)

@jyemin jyemin added the bug label Nov 8, 2017
@evanchooly
Copy link
Member

This appears to work on 2.0. I'm going to revive the 2.0.0-SNAPSHOT publishing (it normally happens automatically with every commit but some quality checks are fading.) If you feel up for testing it against 2.0 (even in a test project), I'd love the feedback.

@evanchooly
Copy link
Member

snapshot builds are pushing again and should be regularly updated after each build should you want to give them a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants