micronaut-data
micronaut-data copied to clipboard
`@DateCreated` won't work when defined in abstract base class from a library
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
- create a multi project setup
- create a library subproject with an abstract class as below
abstract class BaseEntity {
@DateCreated
Instant created
}
- create an application subproject with an entity that extends the class
@TupleConstructor
class FailingTestEntity extends BaseEntity {
@Id
long id
String name
}
- 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
Making the field protected makes the test pass.
Making the field
protectedmakes the test pass.
But that's not very groovy ;)
Will there be a fix?
@graemerocher WDYT?
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).
This is a bug. Groovy users shouldn't have to move away from Groovy standard practices
undoubtedly it is a bug, but where is the bug in core or data?
@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…
@dstepanov Do these annotations simply miss the @Inherited annotation (see here)?
@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?