GoogleCloudPlatform / google-cloud-spanner-hibernate

The official Google Cloud Spanner Dialect for Hibernate ORM
GNU Lesser General Public License v2.1
51 stars 40 forks source link

Verification that the composite key for an interleaved class is a super set of the parent class primary key is broken #482

Open gaeshi opened 1 year ago

gaeshi commented 1 year ago

How to reproduce:

  1. Run basic-hibernate-sample.

The following WARN log appears:

WARN: Composite key for Interleaved table 'com.example.entities.Album' should be a superset of the parent's key.

However, the tables are created fine and the code seems to run fine.

Looks like com.google.cloud.spanner.hibernate.schema.SchemaUtils.resolveIdFields() isn't resolving the actual Id fields of the parent entity in case @IdClass is used.

elefeint commented 1 year ago

@gaeshi Is the only issue the false-positive warning? Or is some functionality affected, too?

gaeshi commented 1 year ago

@elefeint

It does look like the code is running fine.

I tried looking at the InterleavedTest, thinking that I'm not using the library properly, but unfortunately there were only the tests for @EmbeddedId.

The example in the README.md , suggests using IdClass instead:

@Entity
@Interleaved(parentEntity = Singer.class, cascadeDelete = true)
@IdClass(AlbumId.class)
public class Album {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Type(type = "uuid-char")
  private UUID albumId;

  @Id
  @ManyToOne
  @JoinColumn(name = "singerId")
  @Type(type = "uuid-char")
  private Singer singer;

  // Constructors, getters/setters

  public static class AlbumId implements Serializable {

    // The primary key columns of the parent entity
    // must be declared first.
    Singer singer;

    @Type(type = "uuid-char")
    UUID albumId;

    // Getters and setters
  }
}
@Entity
public class Singer {

  @OneToMany(mappedBy = "singer")
  List<Album> albums;

  // continued...
}

But I couldn't find any tests for this.

gaeshi commented 1 year ago

Here is a minimal PoC to reproduce the issue: https://github.com/mixefy/spanner-hibernate-interleaved-poc

elefeint commented 1 year ago

@olavloite @JoeWang1127 FYI.