【问题标题】:JPA issue: trouble with persisting an association class that has an EmbeddedId composite keyJPA 问题:持久化具有 EmbeddedId 复合键的关联类时遇到问题
【发布时间】:2020-05-03 21:58:48
【问题描述】:

我是学习 JPA 的新手,希望对以下内容有所了解:我无法使用 JPA 将关联类的行保存到其各自的表中。

关联类“movie_showings”是一对多,一侧是“电影”,另一侧是一对多,另一侧是“剧院”。可以想象,有一个由后两个类的外键组成的复合主键。我选择使用 Embeddable 类“movie_showings_pk”来实现它。

此时,实例化一个关联类的对象,传递给它一个电影和一个剧院来构造一个PK已经不是问题了。调用函数将 'movie_showing' 实例分别关联到 'theaters' 和 'movies' 不是问题。但是,当我使用 entityManager 去 PERSIST 数据库中的“movie_showings”实例时,我得到一个我不太明白的约束违规异常:

线程“主”javax.persistence.RollbackException 中的异常:异常 [EclipseLink-4002](Eclipse 持久性服务 - 2.7.1.v20171221-bd47e8f):org.eclipse.persistence.exceptions.DatabaseException 内部异常:java.sql.SQLIntegrityConstraintViolationException:列 'MOVIES_MID' 不能接受 NULL 值。 错误代码:20000 调用:INSERT INTO movie_showings (DATEOFLASTSHOW, OPENINGDATE, MOVIE, THEATER, MOVIE_MID, THEATER_TID) VALUES (?, ?, ?, ?, ?, ?) bind => [6个参数绑定] 查询:InsertObjectQuery(PK => MOVIE ID > 3 THEATER ID > 1 开幕日期:2020 年 1 月 15 日)

这是我用来测试的函数定义:

   private void loadInitialMovieShowings ()
   {
      System.out.println ("LOADING movie_showings...");

      List <Movie> allMovies = entityManager.createNamedQuery(Movie.FIND_ALL, Movie.class).getResultList();
      List <Theater> allTheaters = entityManager.createNamedQuery(Theater.FIND_ALL, Theater.class).getResultList();

      Theater spectrum = null;
      for (Theater theater : allTheaters)
         if (theater.getName().contains("Irvine Spectrum"))
            spectrum = new Theater (theater);

      List <Movie> allStarWars = new ArrayList<>();

      for (Movie movie : allMovies)
         if (movie.getTitle().contains("Star Wars"))
            allStarWars.add(movie);

      for (Movie movie : allStarWars)
      {
         MovieShowing temp = new MovieShowing (movie, spectrum,
                 new GregorianCalendar(2020, 00, 15),
                 new GregorianCalendar(2020, 04, 04));

         movie.addMovieShowing(temp);
         spectrum.addMovieShowing(temp);

         entityManager.persist (temp);
      }
   }

这是(部分)关联类:

public class MovieShowing 
{
    // QUERY STRING(S)
    public static final String FIND_ALL_ID = "MovieShowing.FIND_ALL_ID";
    public static final String FIND_ALL_INFO = "MovieShowing.FIND_ALL_INFO";

    @EmbeddedId
    private Movie_Showings_PK ms_id;

    @Temporal (TemporalType.DATE)
    private GregorianCalendar openingDate;

    @Temporal (TemporalType.DATE)
    private GregorianCalendar dateOfLastShow;

    // ASSOCIATION(S)
    @ManyToOne
    private Theater theater;

    @ManyToOne 
    private Movie movie;

    // CONSTRUCTORS
    public MovieShowing () { }
    public MovieShowing (Movie movie, Theater theater, GregorianCalendar opening, GregorianCalendar closing)
    {
        ms_id = new Movie_Showings_PK(movie, theater);

        setMovie(movie);
        setTheater(theater);
        setOpeningDate(opening);
        setDateOfLastShow(closing);
    }

    ...

这是 Embeddedable 类:

public class Movie_Showings_PK
{
    @Basic
    private Long theater;

    @Basic
    private Long movie;

    // CONSTRUCTORS
    public Movie_Showings_PK () {}
    public Movie_Showings_PK (Movie movie, Theater theater)
    {
        this.movie = movie.getMID ();
        this.theater = theater.getTID ();
    }
    ...

我确信答案就在我的脸上,但我已经摸索了几天试图弄清楚...感谢您的任何见解!

【问题讨论】:

    标签: jpa


    【解决方案1】:

    啊。我是个白痴。在我的“电影”类中,我错误地将一个单独的@ManyToMany 关联与“剧院”在之前的尝试中连接这两个表。删除后,一切正常。将此归结为成长的痛苦..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-10
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-26
      • 1970-01-01
      相关资源
      最近更新 更多