我在保存数据库中的实体列表时遇到问题。
我有实体
subdata2 = reactive({
x <- filter(biz1,Stock%in%input$sector, year%in%input$yr)
x
})
有课程
@Entity
@Table(name = "movies")
@Data
public class MovieEntity {
@Id
@Column(unique = true, updatable = false)
@GeneratedValue
private Long id;
@OneToMany(mappedBy = "movie", cascade = CascadeType.ALL)
private Set<MovieDescription> descriptions;
}
继承自
@Entity
@Data
@EqualsAndHashCode(callSuper = true)
@DiscriminatorValue(value = MovieField.Values.DESCRIPTION)
public class MovieDescription extends MovieInfo {
private String description;
}
虽然我在使用这样的代码时使用@Entity
@Table(name = "movies_info")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "dtype")
@Data
public class MovieInfo {
@Id
@Column(unique = true, updatable = false)
@GeneratedValue
private Long id;
@ManyToOne
private MovieEntity movie;
}
cascade = CascadeType.ALL
在没有final MovieEntity movie = new MovieEntity();
movie.setStatus(EditStatus.WAITING);
movie.setTitle(movieDTO.getTitle());
movie.setType(movieDTO.getType());
movieDTO.getDescription().ifPresent(description -> {
MovieDescription movieDescription = new MovieDescription();
movieDescription.setDescription(description);
movie.getDescriptions().add(movieDescription);
});
this.movieRepository.save(movie);
的情况下将对象本身MovieEntity
保存到数据库中。映射列表不会保存到数据库。为什么呢?
答案 0 :(得分:1)
在您的情况下,MovieDescription
类是关系所有者。
@OneToMany(*mappedBy = "movie"*, cascade = CascadeType.ALL)
private Set<MovieDescription> descriptions;
JPA仅与所有者保持关系,但您未在说明中设置MovieEntity
链接
MovieDescription movieDescription = new MovieDescription();
movieDescription.setDescription(description);
movie.getDescriptions().add(movieDescription);
为它添加下一行:
movieDescription.setMovie(movie);