micronaut-data icon indicating copy to clipboard operation
micronaut-data copied to clipboard

Cannot use Embedded / Embeddable properties

Open sflahave opened this issue 2 years ago • 3 comments

Expected Behavior

We have a DiscountEO type that is intended to be embedded into certain entities. Our setup has always worked in the past but fails when trying to upgrade to Micronaut 3.1.1 and related. We expect that it would continue working on the latest version of Micronaut & Micronaut-data.

Actual Behaviour

We are seeing the following error when we run ./gradlew clean build:

 io.micronaut.data.exceptions.MappingException: Type [com.codeswyft.fielder.domain.DiscountEO] of property [discount] of entity [com.codeswyft.fielder.domain.InvoicePO] is missing @Embeddable annotation. @Embedded fields can only be applied to types annotated with @Embeddable
        at io.micronaut.data.processor.model.SourcePersistentEntity.<init>(SourcePersistentEntity.java:90)
        at io.micronaut.data.processor.visitors.MappedEntityVisitor$1.lambda$apply$0(MappedEntityVisitor.java:71)
        at io.micronaut.data.processor.visitors.MappedEntityVisitor$1.apply(MappedEntityVisitor.java:71)
        at io.micronaut.data.processor.visitors.MappedEntityVisitor$1.apply(MappedEntityVisitor.java:68)
        at io.micronaut.data.processor.visitors.MappedEntityVisitor.visitClass(MappedEntityVisitor.java:106)
        at io.micronaut.ast.groovy.visitor.LoadedVisitor.visit(LoadedVisitor.groovy:172)
        at io.micronaut.ast.groovy.TypeElementVisitorTransform$ElementVisitor$_visitClass_closure1.doCall(TypeElementVisitorTransform.groovy:150)
        at io.micronaut.ast.groovy.TypeElementVisitorTransform$ElementVisitor$_visitClass_closure1.call(TypeElementVisitorTransform.groovy)
        at io.micronaut.ast.groovy.TypeElementVisitorTransform$ElementVisitor.visitClass(TypeElementVisitorTransform.groovy:149)
        at io.micronaut.ast.groovy.TypeElementVisitorTransform.visit(TypeElementVisitorTransform.groovy:103)
        ... 28 more

Steps To Reproduce

  1. Checkout this simplified repo: See this simplified repo for yourself: https://github.com/codeswyft/micronaut-test
  2. Run ./gradlew clean build

Environment Information

➜  micronaut-test git:(main) ./gradlew -v                      

------------------------------------------------------------
Gradle 7.2
------------------------------------------------------------

Build time:   2021-08-17 09:59:03 UTC
Revision:     a773786b58bb28710e3dc96c4d1a7063628952ad

Kotlin:       1.5.21
Groovy:       3.0.8
Ant:          Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM:          11.0.9.1 (AdoptOpenJDK 11.0.9.1+1)
OS:           Mac OS X 10.16 x86_64

Example Application

https://github.com/codeswyft/micronaut-test

Version

3.1.1

sflahave avatar Oct 21 '21 18:10 sflahave

Update: It seems that the issue is really that the @Embedded properties are defined on an abstract parent Entity, TransactionPO, which is annotated with @MappedSuperclass. In the example repo, there is only one child of TransactionPO: InvoicePO. If I copy the @Embedded properties into the child entity class definition, then it seems to work. See this PR: https://github.com/codeswyft/micronaut-test/pull/2

In our real app we have several concrete children of TransactionPO. Shouldn't we be able to only define the common properties on the abstract parent, TransactionPO?

sflahave avatar Oct 21 '21 19:10 sflahave

Update: We found that instead of copying the @Embedded properties into every subclass, we could just remove the @Embedded annotation

sflahave avatar Oct 26 '21 14:10 sflahave

I will investigate why is it happening

dstepanov avatar Oct 27 '21 04:10 dstepanov

Please try with Micronaut 4, it's not throwing compilation errors and it might have been fixed by the https://github.com/micronaut-projects/micronaut-core/issues/6430

radovanradic avatar Sep 13 '23 12:09 radovanradic