0

I'm using spring-boot-starter-data-jdbc 2.4.2. In my domain aggregate I need to map a List of Strings that is populated from a column in another table. It is a legacy database so I have no control over the table and column names and need to use custom names. I see there is an @MappedCollection annotation, but can't see how to use it in this scenario. Below is my class:

@Data
@Table("NMT_MOVIE_THEATRE")
public class MovieTheatre {

    @Id
    @Column("MOVIE_THEATRE_ID")
    private Long id;

    @Column("ZIP_CODE")
    private String zipCode;

    // this comes from table NMT_CURRENT_MOVIE, column CM_ID, joined by MOVIE_THEATRE_ID
    private List<String> currentMovieIds;
}

Using Spring Data JDBC, how can I create the one-to-many relation?

CeeTee
  • 778
  • 1
  • 9
  • 17

1 Answers1

3

Wrap your String in a little entity.

@Table("NMT_CURRENTMOVIE")
class MovieId {
    @Id
    @Column("CM_ID")
    final String id

    // add constructor, equals and hashCode here or generate using Lombok
}

Then use it in the MovieTheatre. Since you don't have a column for an index, the proper collection to use is a Set

// ...
class MovieTheatre {
    // ...
    @MappedCollection(idColumn="MOVIE_THEATRE_ID")
    Set<MovieId> currentMovieIds;
}

Note that equals and hashCode is important as well as the constructor taking all arguments used in those, since the entity is used in a Set.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348