Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to implement a POJO in Spring Boot that has a field representing a composite primary key while not utilizing JPA or nested classes?

I am currently trying to implement R2DBC within Spring Boot for an application that is already far into development - meaning, unfortunately, our DDL is not flexible, since other microservices depend on this it and vice versa. One significant limitation of R2DBC is that is does not support nested classes. Moreover, it does not support JPA, since that would would defeat the purpose of being all-around non-blocking. So all those useful annotations are off the table as well.

The class I am dealing with looks something like this:

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString

public class Stats {
   @Id
   StatsPK statsPK

   @Column(stat_name)
   String stat;

   ...

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Embeddable
    public static class StatsPK implements Serializable {
        @Column("store_id")
        private Integer storeId;

        @Column("empl_id")
        private String emplId;

        @Column("app_version")
        private String appVersion;
    }
}

After researching my question, I have found many ways to go about accounting for the composite key, including what is already been shown in the example above (using the @Embeddable annotation), using a custom PK object (which is similar, but would have the option of using a class designed for this purpse, such as CompositeKeyHolder), The JPA @TableId(TableId.class) annotation, and some others that seemed less promising.

All of these options seem to either be utilizing JPA or utilizing a nested object, which I cannot do. I can't really think of a way to get around these limitations. But since I'm a beginner, I decided to ask in case anyone has dealt with this issue before. Appreciate any feedback.

like image 556
Christian Meyer Avatar asked Oct 17 '22 05:10

Christian Meyer


1 Answers

Spring Data R2DBCs repository abstraction does not support composite keys yet. What you could use the DatabaseClient though to construct your SQL and to query data.

like image 134
Jens Schauder Avatar answered Oct 29 '22 14:10

Jens Schauder