Skip to content

Add compile-time type validation for @SnowflakeGeneratedId #3

Description

@Fayupable

Currently @SnowflakeGeneratedId can be applied to a field or getter of any type. The generator always produces a long value, so if the annotated field is not Long or long (for example String, Integer, or UUID), the code compiles without any warning and the mistake only surfaces at runtime, the first time Hibernate tries to assign the generated value to the entity and fails.

This should be caught at compile time instead. Add an annotation processor to the snowflake-jpa module that scans for elements annotated with @SnowflakeGeneratedId and emits a compilation error if the annotated field or getter return type is not Long or long.

Requirements:

  • Handle both placements allowed by the annotation target, field and getter method.
  • Accept both the primitive long and the boxed Long type.
  • Emit a clear compiler error message identifying the offending element and its actual type.
  • Register the processor through the standard Java service loader mechanism so it activates automatically when the snowflake-jpa dependency is on the compile classpath, without requiring consumers to add extra configuration in most setups.
  • Document in the README that some Maven Compiler Plugin versions may require an explicit annotationProcessorPaths entry for the processor to run, since this is not guaranteed to auto activate in every build tool configuration.
  • Add tests that compile a small in-memory source file with a wrong-typed field and assert that compilation fails with the expected error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions