DEV Community

Edgar Rios Navarro
Edgar Rios Navarro

Posted on

1 1

PK compuesta con Micronaut-R2DBC

Con Micronaut-Data, tenemos ComposeId out-of-the-box. El manejo es muy similar que JPA.

La tabla de ejemplo contiene una llave primaria formada por dos campos.

CREATE TABLE "author_detail_entity" (
"author_id" BIGINT NOT NULL,
"book_id" BIGINT NOT NULL, 
PRIMARY KEY("author_id","book_id"));
Enter fullscreen mode Exit fullscreen mode

Debemos usar las anotaciones provistas para tal fin: EmbeddedId, Embeddable.

import io.micronaut.data.annotation.*;

@MappedEntity
public class AuthorDetailEntity {
    @EmbeddedId
    private AuthorDetailId authorDetailId;

    public AuthorDetailEntity(AuthorDetailId authorDetailId) {
        this.authorDetailId = authorDetailId;
    }

    public AuthorDetailId getAuthorDetailId() {
        return authorDetailId;
    }
}

@Embeddable
class AuthorDetailId {
    @MappedProperty("author_id")
    private final Long authorId;
    @MappedProperty("book_id")
    private final Long bookId;

    public AuthorDetailId(Long authorId, Long bookId) {
        this.authorId = authorId;
        this.bookId = bookId;
    }

    public Long getAuthorId() {
        return authorId;
    }

    public Long getBookId() {
        return bookId;
    }
}
Enter fullscreen mode Exit fullscreen mode

Extendemos la interface para obtener un CRUD reactivo.

@R2dbcRepository(dialect = Dialect.POSTGRES)
public interface AuthorDetailRepository 
        extends ReactiveStreamsCrudRepository<AuthorDetailEntity, AuthorDetailId> {
}
Enter fullscreen mode Exit fullscreen mode

Algunos métodos provistos son find*, delete*, etc.
alt text
alt text


Documentación

https://micronaut-projects.github.io/micronaut-data/lates/guide/#sqlCompositeId
https://gitlab.com/edgar.gs/mn-r2dbc.git

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Postgres on Neon - Get the Free Plan

No credit card required. The database you love, on a serverless platform designed to help you build faster.

Get Postgres on Neon

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay