Comments (9)
What's the use case for doing a projection on the discriminator? Such metadata should be ignored/transparent to your application?
from morphia.
I have an Entity which contains a property that is an interface. When I add a projection on the fields of one of the subtypes of the interface, it doesn't include the discriminatorKey to the projection. As a consequence the InstanceCreatorFactory
throws out an error as it's can't find a constructor for the interface because the discriminator is missing.
The Projection
class does include the following snippet:
if (isIncluding() && entityAnnotation != null && entityAnnotation.useDiscriminator()) { projection.put(mapper.getConfig().discriminatorKey(), 1); }
But this only gets applied to the main Entity and not it's nested interface.
from morphia.
If you're querying by the interface type, you'll need to enable polymorphic queries.
from morphia.
The parent entity is not an interface, so from what I understood, it wouldn't be considered a polymorphic query.
The query is on the parent entity which is a concrete class, the interface is a property of the entity.
@Entity
public class MyEntity {
@Id private ObjectId myId;
@Property private MyInterface prop;
}
@Entity( useDiscriminator = true )
public interface MyInterface {}
@Entity( useDiscriminator = true, discriminator = "mydisc" )
public class MyImpl implements MyInterface {
@Property private int propA;
... more properties
}
The query is on MyEntity and I am adding a projection on the properties in MyImpl e.g. "prop.propA"
In this case the Morphia does not include the discriminatorKey into the projection and so the decoding of the MyInterface property fails.
from morphia.
What does your query look like?
from morphia.
I've tried to replicate the query as closely as possible in terms of filters and options used. Obviously not all the fields match above, but I've tried to keep the entity and embedded entity references accurate.
Query<MyEntity> query = datastore.find(MyEntity.class)
.disableValidation()
.filter( eq( "propertyA", someValue ) )
.filter( in( "propertyB", listOfValues ) )
.filter( gte( "datePropertyA", startDate ) )
.filter( lte( "datePropertyA", endDate ) )
.filter( ne( "propertyC", otherValue ) );
FindOptions options = new FindOptions()
.sort( Sort.descending( "datePropertyA" ) )
.projection().include( "prop.mydisc", "prop.propA" )
.skip( 0 )
.limit( 1 );
query.iterator( options ).toList();
from morphia.
I'm not seeing how/where the discrimininator come in to play. Is the failure you're seeing that the projection doesn't know how to "see past" the interface definition?
from morphia.
If I include the discriminator in the projection, then the PathTarget
throws a ValidationException
due to an invalid path.
If I don't include the discriminator in that projection, then the InstanceCreatorFactory
throws a MappingException
with the error "No suitable constructor found for type".
from morphia.
If you could put together a reproducer I can take a deeper look. I have some ideas about what might be happening here but I can't quite follow your examples here well enough to be confident in recreating your scenario.
from morphia.
Related Issues (20)
- Why is annotating mappable classes necessary?
- Add jakarta-data support
- How do I register a custom codec HOT 1
- Using @Reference annotation on map value error
- Add a validator to prevent mapping one property's name to match the field of another
- Update docs parser
- New release of 1.6 branch with current mongo-driver HOT 2
- dev.morphia.Datastore Thread Safety HOT 4
- Prefer to load custom property codec providers before default codec providers.
- why `Query#iterator` return `dev.morphia.query.internal.MorphiaCursor` instead of `dev.morphia.query.MorphiaCursor` HOT 3
- upsert of $inc with map of map throwing ValidationException HOT 1
- jakarta.validation.ValidationException: HV000183: Unable to initialize 'jakarta.el.ExpressionFactory'. Check that you have the EL dependencies on the classpath, or use ParameterMessageInterpolator instead HOT 1
- Implement aggregation operator $querySettings
- Implement aggregation operator $or
- Implement aggregation operator $size
- Implement aggregation operator $not
- Implement aggregation operator $jsonSchema
- Implement aggregation operator $geometry
- Why are my Morphia Cursors stacking up ? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from morphia.