micronaut-projects / micronaut-projects/micronaut-data

Add support for MapsId annotation

Open
#114 6 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: validated type: enhancement
Dominant language
Java
Stars
482
Forks
229
Avg merge
1d 7h
Merged PRs (30d)
32

Description

I can't make @MapsId work using Micronaut data JDBC and Kotlin.

SQL schema :

CREATE TABLE user_account (
    id BIGINT NOT NULL PRIMARY KEY,
    username VARCHAR(255) NOT NULL,
);
CREATE TABLE user_tokens (
    id BIGINT NOT NULL PRIMARY KEY REFERENCES user_account(id),
    access_token VARCHAR(255) NOT NULL,
    refresh_token VARCHAR(255) NOT NULL,
    expiration_date TIMESTAMP NOT NULL
);

Kotlin classes :

import javax.persistence.*

@Entity
data class UserAccount(
        @Id
        val id: Long,
        @Column(nullable = false)
        val username: String
)

@Entity
data class UserTokens(
        @Id
        val id: Long,
        @MapsId
        @OneToOne
        val user: UserAccount,
        @Column(nullable = false)
        val accessToken: String,
        @Column(nullable = false)
        val refreshToken: String,
        @Column(nullable = false)
        val expirationDate: Date
)

Repositories :

@JdbcRepository
interface UserAccountRepository : CrudRepository<UserAccount, Long>
@JdbcRepository
interface UserTokensRepository : CrudRepository<UserTokens, Long>

Inserting a new user account works well, but when trying to insert some associated tokens, SQL query is generated using a user_id property I do not expect here.

io.micronaut.data.exceptions.DataAccessException: SQL Error executing INSERT: Colonne "USER_ID" non trouvée
Column "USER_ID" not found; SQL statement:
INSERT INTO user_tokens (user_id,access_token,refresh_token,expiration_date,id) VALUES (?,?,?,?,?) [42122-199]

I'm expecting the target SQL request to be
INSERT INTO user_tokens (access_token,refresh_token,expiration_date,id) VALUES (?,?,?,?)
Since @MapsId + @OneToOne should just declare a primary + foreign key.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the Kotlin UserAccount and UserTokens entities, the @MapsId and @OneToOne annotations, and the two JDBC repositories shown here. Trace the generated INSERT for user_tokens and the handling of @MapsId; done means it no longer references the nonexistent user_id column and matches the stated schema.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, kotlin, sql
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.