Code Monkey home page Code Monkey logo

Comments (9)

evanchooly avatar evanchooly commented on August 19, 2024

What's the use case for doing a projection on the discriminator? Such metadata should be ignored/transparent to your application?

from morphia.

agiannone avatar agiannone commented on August 19, 2024

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.

evanchooly avatar evanchooly commented on August 19, 2024

If you're querying by the interface type, you'll need to enable polymorphic queries.

from morphia.

agiannone avatar agiannone commented on August 19, 2024

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.

evanchooly avatar evanchooly commented on August 19, 2024

What does your query look like?

from morphia.

agiannone avatar agiannone commented on August 19, 2024

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.

evanchooly avatar evanchooly commented on August 19, 2024

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.

agiannone avatar agiannone commented on August 19, 2024

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.

evanchooly avatar evanchooly commented on August 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.