Mam taką encję:
@Entity
public class Screening {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "screening_gen")
@SequenceGenerator(name = "screening_gen", sequenceName = "sc_seq", initialValue = 1001)
private Long id;
@NotNull
@OneToOne
private Movie movie;
}
Napisałem dla niej DTO:
public class ScreeningDTO {
private Long id;
private Movie movie;
}
Jednak kiedy próbuję mapować z DTO to dostaję:
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='must not be null', propertyPath=movie, rootBeanClass=class model.Screening, messageTemplate='{javax.validation.constraints.NotNull.message}'}]]
2020-01-13 10:20:22.931 ERROR 28418 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction] with root cause
Próbowałem już kilku rozwiązań znalezionych w sieci, ale bez skutku. Stąd moje pytanie jak powinienem to zrobić?
Inicjalizuję dane wysyłając POSTa:
{
"movie": {
"id": 1,
"title": "The Shawshank Redemption",
"category": "drama"
}
}
@Entity
public class Movie {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@Size(min = 3)
private String title;
@Size(min = 3)
@NotEmpty
private String category;
}