Issues converting to 2.x
See original GitHub issueI have been running on Morphia for many years. I am trying to migrate my codebase from 1.6.x to 2.1 and am running into some issues. Primarily I cannot get it to initialize. In 1.6, I initialized like this:
Morphia morphia = new Morphia();
// auto-embedded indices are going away; this removed the warning messages in log
morphia.getMapper().getOptions().setDisableEmbeddedIndexes(true);
morphia.map(UserBean.class);
morphia.map(RequestBean.class);
morphia.map(PasswordResetBean.class);
morphia.map(NdcProduct.class);
morphia.map(Hcpcs.class);
morphia.map(LookupBean.class);
morphia.map(DiseaseBean.class);
morphia.map(DiseaseVersionBean.class);
morphia.map(DiseaseChangelogBean.class);
morphia.map(RxBean.class);
morphia.map(RxVersionBean.class);
morphia.map(RxChangelogBean.class);
morphia.map(GlossaryBean.class);
morphia.map(GlossaryVersionBean.class);
morphia.map(GlossaryChangelogBean.class);
morphia.map(StagingVersion.class);
morphia.map(StagingSchema.class);
morphia.map(StagingTable.class);
morphia.map(StagingSchemaHistory.class);
morphia.map(StagingTableHistory.class);
MongoClient mongoClient = new MongoClient(new MongoClientURI(uri));
Datastore datastore = morphia.createDatastore(mongoClient, db);
datastore.ensureIndexes();
Looking at the documentation at https://morphia.dev/morphia/2.1/quick-tour.html, I tried to initialize 2.1 like this:
Datastore datastore = Morphia.createDatastore(MongoClients.create("mongodb://localhost:27017"), "seerapi_unittest",
MapperOptions.builder().disableEmbeddedIndexes(true).build());
datastore.getMapper().map(UserBean.class);
datastore.getMapper().map(RequestBean.class);
datastore.getMapper().map(PasswordResetBean.class);
datastore.getMapper().map(NdcProduct.class);
datastore.getMapper().map(Hcpcs.class);
datastore.getMapper().map(LookupBean.class);
datastore.getMapper().map(DiseaseBean.class);
datastore.getMapper().map(DiseaseVersionBean.class);
datastore.getMapper().map(DiseaseChangelogBean.class);
datastore.getMapper().map(RxBean.class);
datastore.getMapper().map(RxVersionBean.class);
datastore.getMapper().map(RxChangelogBean.class);
datastore.getMapper().map(GlossaryBean.class);
datastore.getMapper().map(GlossaryVersionBean.class);
datastore.getMapper().map(GlossaryChangelogBean.class);
datastore.getMapper().map(StagingVersion.class);
datastore.getMapper().map(StagingSchema.class);
datastore.getMapper().map(StagingTable.class);
datastore.getMapper().map(StagingSchemaHistory.class);
datastore.getMapper().map(StagingTableHistory.class);
datastore.ensureIndexes();
It fails on the first line with the following exception:
java.lang.ArrayStoreException: dev.morphia.mapping.codec.PrimitiveCodecRegistry
at dev.morphia.mapping.Mapper.<init>(Mapper.java:95)
at dev.morphia.DatastoreImpl.<init>(DatastoreImpl.java:68)
at dev.morphia.Morphia.createDatastore(Morphia.java:48)
at com.imsweb.seerapi.MorphiaConfigurationTest.testInit(MorphiaConfigurationTest.java:36)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
I assume there is a problem with one of my mappings, but I have no idea which. I haven’t changed these mapping in a long time and the only changes I made to the mappings are the following (this was done in many classes, but I am showing just one example):
Removed noClassnameStored
since it is no longer supported.
@Entity(value = "users", noClassnameStored = true)
became
@Entity(value = "users")
Changed the @Embedded
annotation.
@Embedded("keys")
private Collection<UserKeyBean> _keys;
became
@Property("keys")
private Collection<UserKeyBean> _keys;
Is there a way to determine what mapping it does not like?
On a side note, I found a typo in the documentation for “Limiting and Skipping” (https://morphia.dev/morphia/2.1/querying.html#_limiting_and_skipping). The example looks like this:
datastore.createQuery(Person.class)
.iterator(new FindOptions()
.offset(1)
.limit(10))
However no offset
method exists. The method name is skip
in FindOptions
.
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (4 by maintainers)
I didn’t think of trying
@Embedded
. It worked. Thanks for the help!You’ll need to mark it with
@Entity
( or@Embedded
) so that Morphia knows to map it out. 2.2 simplifies this a little by basically making@Entity
the only annotation you need to use (@Embedded
will be deprecated). Then for those types that will never serve as queryable types (i.e., mapped to a collection), you can skip the@Id
annotation on a field. But that wont’ be available until 2.2 is out. ( I’m hoping in the next couple of weeks. there’s only 1 remaining issue.)So try using
@Embedded
for now and that should resolve your issue here. If not, we can look at next steps but I’d be surprised if it that didn’t do the trick.