eclipse / jnosql

Eclipse JNoSQL is a framework which has the goal to help Java developers to create Jakarta EE applications with NoSQL.
Other
231 stars 72 forks source link

Support for Polymorphism #262

Closed amoscatelli closed 1 year ago

amoscatelli commented 2 years ago

I believe we should support polymorphism, at least in KeyValue and Document abstraction.

If I have a MongoDB (or other schema less Document databases) Collection for Animals, I can store a Cat, a Dog (etc etc) modeled/annotated as Document Entities inside that collection. They can have different exclusive attributes but that should not be a problem since we are schema less.

The same applies to KeyValue abstraction.

This could resemble or can be compared to the SingleTable JPA Inheritance strategy.

The issue here is that as far as I understand in the current abstraction we do not propagate the base java type, so we can't manage to serialize any kind of discriminator field/attribute into the database.

Have you already discussed about this ? I would be glad to help if we agree this is important.

redmitry commented 2 years ago

Probably the right way to do this (in JSON) would be via JSON Schema "oneOf": [] and then "try and get" as schema says only one of a set of schemas must be compatible with the object. Unfortunately, no standard JSON Schema API exists in Java. JSON-B 3.0 introduced @JsonbTypeInfo but, IMHO, this is not good way to go. For instance I already have Json Schema and MongoDB database I am mapping, no luck. I like custom serializers / deserializers in JSON-B 1/2 and would be happy @JsonbTypeDeserializer be supported in the JNoSQL. The problem here is that @JsonbTypeDeserializer is based on JsonParser while, for instance, MongoDB uses BSON. I think it's still possible to get the Json wrapper from mongo driver though.

D.

amoscatelli commented 2 years ago

Jsonb 3.0 provides support for polymorphism out of the box now.

After 4 years ...

https://github.com/eclipse-ee4j/yasson/issues/133 https://github.com/eclipse-ee4j/jsonb-api/issues/147

AFAIK Jakarta 10 will include Jsonb 3.0

amoscatelli commented 2 years ago

I think KEYVALUE polymorphism can be achieved with this only.

DOCUMENT requires the introduction of a type discriminator 'document'.

redmitry commented 2 years ago

According the spec. it uses discriminator (aka type) property: https://jakarta.ee/specifications/jsonb/3.0/jakarta-jsonb-spec-3.0.html#polymorphic-types I think you can't have "true" polymorphism without Json Schema as you must decide which type match better (or in a case "oneOf" which one matches).

amoscatelli commented 2 years ago

Doesn't discriminator property do exactly that ? By polymorphism I mean that if I use JNoSQL to store a Cat in the Animal database, then I can retrieve it as a Cat, not a generic Animal. Discriminator property value must be unique of course. We don't need to decide which type match metter. We know the exact type via discriminator value.

redmitry commented 2 years ago

Yes, unless you do not map already existing data with no discriminator. I'd be happy to have a support for "@JsonbTypeAdapter".

otaviojava commented 2 years ago

Sorry for the huge delay. Right now, we have support for it using the same JPA style.