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

`@DateCreated` won't work when defined in abstract base class from a library

Open sascha-frinken opened this issue 3 years ago • 15 comments

Expected Behavior

An entity inheriting a class with a property that is annotated with @DateCreated should work even if the abstract base class is defined in a library (dependency).

Actual Behaviour

The entity cant't be saved as the database complains that the created column is null.

Steps To Reproduce

  1. create a multi project setup
  2. create a library subproject with an abstract class as below
abstract class BaseEntity {

    @DateCreated
    Instant created
}
  1. create an application subproject with an entity that extends the class
@TupleConstructor
class FailingTestEntity extends BaseEntity {
    @Id
    long id
    String name
}
  1. try to save the entity
failingRepository.save(new FailingTestEntity(1, "test"))

This will fail with the following error:

Caused by: io.micronaut.data.exceptions.DataAccessException: SQL Error executing INSERT: NULL not allowed for column "CREATED"; SQL statement: INSERT INTO failing_test_entity (name,created,id) VALUES (?,?,?) [23502-200]

This is the io.micronaut.data.query log:

22:55:44.553 [Test worker] DEBUG io.micronaut.data.query - Executing SQL Insert: INSERT INTO `failing_test_entity` (`name`,`created`,`id`) VALUES (?,?,?)
22:55:44.556 [Test worker] TRACE io.micronaut.data.query - Binding parameter at position 1 to value test with data type: STRING
22:55:44.557 [Test worker] TRACE io.micronaut.data.query - Binding parameter at position 2 to value null with data type: TIMESTAMP
22:55:44.557 [Test worker] TRACE io.micronaut.data.query - Binding parameter at position 3 to value 1 with data type: LONG

Environment Information

  • OS: Linux:
  • JDK: 11

Example Application

https://github.com/sascha-frinken/micronaut-data-issues-1282

Version

3.2.7

sascha-frinken avatar Jan 20 '22 22:01 sascha-frinken

Making the field protected makes the test pass.

dstepanov avatar Jan 21 '22 05:01 dstepanov

Making the field protected makes the test pass.

But that's not very groovy ;)

Will there be a fix?

sascha-frinken avatar Jan 21 '22 08:01 sascha-frinken

@graemerocher WDYT?

dstepanov avatar Jan 21 '22 09:01 dstepanov

If you add protected then you're creating a field, and no accessors anymore. If you remove the modifier like in the bug report, then you're creating 3 things:

  • a private field
  • a getter
  • a setter

So the problem is likely that the annotation ends up on a private field (which, if I recall, is Groovy's behavior when you annotate a property).

melix avatar Jan 24 '22 08:01 melix

This is a bug. Groovy users shouldn't have to move away from Groovy standard practices

jameskleeh avatar Jan 24 '22 14:01 jameskleeh

undoubtedly it is a bug, but where is the bug in core or data?

graemerocher avatar Jan 25 '22 13:01 graemerocher

@graemerocher Sorry I don't want to stress here. But it would be enough just to know if you are planning a fix for this? We have a workaround for this - it just means a lot of refactoring…

sascha-frinken avatar Feb 07 '22 12:02 sascha-frinken

@dstepanov Do these annotations simply miss the @Inherited annotation (see here)?

sascha-frinken avatar Feb 17 '22 09:02 sascha-frinken

@graemerocher @jameskleeh @dstepanov Sorry for my annoyance, but this is still important for us. We have a lot of applications with a lot of entities inheriting a BaseEntity that brings a @DateCreated and a @DateUpdated property. Will this be fixed?

sascha-frinken avatar Apr 05 '22 18:04 sascha-frinken