JPA - 不能全部使用cascada类型

时间:2017-10-12 18:21:26

标签: java spring jpa spring-boot eclipselink

我在保存数据库中的实体列表时遇到问题。

我有实体

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保存到数据库中。映射列表不会保存到数据库。为什么呢?

1 个答案:

答案 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);