1

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?

asked Jan 26, 2021 at 9:05

1 Answer 1

5

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.

answered Jan 26, 2021 at 9:26
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.