skip to Main Content

I am trying to save one-to-many entities to PostgreSQL database using Spring Boot and Spring Data JDBC.

Main application code:

@SpringBootApplication
public class TvApplication
{
    @Autowired
    private SeasonRepository seasonRepository;
    @EventListener(ApplicationReadyEvent.class)
    public void saveToDatabase()
    {
        Episode episode1 = new Episode("firstEpisode");
        Episode episode2 = new Episode("secondEpisode");
        Season season = new Season("firstSeason");
        season.addEpisode(episode1);
        season.addEpisode(episode2);
        seasonRepository.save(season);
    }
}

Season class:

@Table("season")
public class Season
{
    @Id
    @Column("id")
    private long id;
    @Column("name")
    private String name;
    @MappedCollection(idColumn = "id", keyColumn = "seasonid")
    private List<Episode> episodes;
    public Season(long id, String name, List<Episode> episodes)
    {
        this.id = id;
        this.name = name;
        this.episodes = episodes;
    }
    public Season(String name)
    {
        this(0, name, new ArrayList<>());
    }
    public void addEpisode(Episode episode)
    {
        episodes.add(episode);
    }

Episode class:

@Table("episode")
public class Episode
{
    @Id()
    @Column("id")
    private long id;
    @Column("name")
    private String name;
    public Episode(long id, String name)
    {
        this.id = id;
        this.name = name;
    }
    public Episode(String name)
    {
        this(0, name);
    }

What I get is this exception:

org.postgresql.util.PSQLException: ERROR: insert or update on table "episode" violates foreign key constraint "episode_seasonid_fkey"
Detail: Key (seasonid)=(0) is not present in table "season"

I think that Spring cannot read auto-generated key from table "season".

2

Answers


  1. The id of a new entity is -1 instead of zero or you can leave it as null.

    Update the code to the following:

    public Season(String name)
    {
        this(-1L, name, new ArrayList<>());
    }
    
    public Episode(String name)
    {
        this(-1L, name);
    }
    
    Login or Signup to reply.
  2. try change like this.

          @MappedCollection(idColumn = "seasonid")
            private Set< Season > season = new HashSet<>();
    
      public void addSeason(Season s) {
            this.season.add(s);
        }
    

    then episode

        Table("episode")
    public class Episode
    {
        @Id
        private long id;
        /// rest of the stuff
    }
    

    remove this part -> this(0, name, new ArrayList<>());

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search