'Spring Boot JPA: One To One Shared Key Issue In Kotlin
I cannot share the parent objects primary key with the child object in a One-To-One Unidirectional Relationship using Kotlin.
I've seen this issue solved in java with the following code:
MovieDetail movieDetail = new MovieDetail();
movie.setMovieDetail(movieDetail);
movieDetail.setMovie(movie); //<-- This is required
MovieRepository.save(movie);
That codes states that the MovieDetail needs a Movie object, because the Movie object contains the id that the MovieDetail will adopt.
However, in Kotlin it turns into a circular dependency where Movie requires a MovieDetail, and MovieDetail requires a Movie, and that Movie requires a MovieDetail and so on.
I don't know how to resolve this in Kotlin.
Here is my code:
@Entity
@Table(name = "movie")
data class Movie(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, updatable = false)
val id: Int,
@Column(name = "movie_id", nullable = false, columnDefinition = "TEXT")
val movieId: String,
@OneToOne(cascade = [CascadeType.ALL])
@PrimaryKeyJoinColumn
var movieDetail: MovieDetail
)
@Entity
@Table(name = "movie_detail")
data class MovieDetail(
@Id
@Column(name = "id")
val id: Int,
@Column(name = "movie_id")
val movieId: String,
@Column(name = "title")
val title: String
@MapsId
@OneToOne
@JoinColumn(name = "id")
var movie: Movie
)
class MovieDataSourceImpl(private val movieRepo: MovieRepo) : MovieDataSource {
...
override fun createMovie(movie: Movie): Movie {
return if (movieRepo.findById(movie.id).isEmpty) {
val movieDetail = movie.movieDetail
movieDetail.movie = movie
movie.movieDetail = movieDetail
movieRepo.save(movie)
} else {
throw IllegalArgumentException("A movie with the 'id' ${movie.id} already exists")
}
}
...
}
Here is the body of the POST request I'm making:
{
"movieId": "1003",
"posterPath": "myPosterPath",
"movieDetail": {
"movieId": "1003",
"title": "title"
}
}
Here is the error that I get:
It is complaining that a Movie object is missing from MovieDetail, this is the circular dependency that I mentioned.
JSON parse error: Instantiation of [simple type, class com.tinnovakovic.springboot.fluttermovierest.model.MovieDetail] value failed for JSON property movie due to missing (therefore NULL) value for creator parameter movie which is a non-nullable type; nested exception is com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException: Instantiation of [simple type, class com.tinnovakovic.springboot.fluttermovierest.model.MovieDetail] value failed for JSON property movie due to missing (therefore NULL) value for creator parameter movie which is a non-nullable type
at [Source: (PushbackInputStream); line: 19, column: 5] (through reference chain: com.tinnovakovic.springboot.fluttermovierest.model.Movie["movieDetail"]->com.tinnovakovic.springboot.fluttermovierest.model.MovieDetail["movie"])]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
