How are many to many relationships in Cloud Spanner used using Spring Boot without Hibernate

22 views Asked by At

The Cloud Spanner Library for Java Spring Boot states the following:

While one-to-one and many-to-many relationships can be implemented in Cloud Spanner and Spring Data Cloud Spanner using constructs of interleaved parent-child tables, only the parent-child relationship is natively supported.

However I could not find any solution on how to actually implement this many-to-many relationship in my project.

With the Hibernate Dialect the @ManyToMany annotation would solve this problem. But how does someone implement it purely with the Google Spanner Data Library?

If I use the @Interleaved annotation on both entities that have the relationship, the join table needs to have the @PrimaryKey in the same order as those entities. But because both entities have the Primary Key on the keyOrder = 1, one of them is in the wrong order

com.google.cloud.spring.data.spanner.core.mapping.SpannerDataException: The child primary key column (JoinTable.ID2) at position 1 does not match that of its parent (Entity2.ID2)

@Table
public class Entity1 {

    @PrimaryKey(keyOrder = 1)
    private UUID ID1;

    @Interleaved
    private List<JoinTable> entities2;

}

@Table
public class Entity2 {

    @PrimaryKey(keyOrder = 1)
    private UUID ID2;

    @Interleaved
    private List<JoinTable> entities1;

}

@Table
public class JoinTable {

    @PrimaryKey(keyOrder = 1)
    private UUID ID1;
    @PrimaryKey(keyOrder = 2)
    private UUID ID2;

}
0

There are 0 answers