Home > Software design >  One To Many relation spring boot, error relation "<xyz>" does not exist
One To Many relation spring boot, error relation "<xyz>" does not exist

Time:11-21

I try to create a relation between two tables in a Spring Boot Application. I have the following Code:

    @Entity
    @Table(name = "account")
    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    public class Account {
      @Id
      @Column(name="ID", nullable = false, updatable = false)
      @GeneratedValue(strategy=GenerationType.SEQUENCE)
      private Long id;
      @Column(name="NAME", nullable = false)
      private String name;
      @Column(name="client_id", nullable = false)
      private Long clientId;
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "client_id", insertable=false, updatable=false)
      private Client client;
    }

    @Entity
    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    public class Client {
      @Id
      @Column(name="client_id", nullable = false, updatable = false)
      @GeneratedValue(strategy=GenerationType.SEQUENCE)
      private Long id;
      @Column(name="NAME", nullable = false, unique = true)
      private String name;
      @OneToMany(mappedBy = "client", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
      private List<Account> accounts;
      }

I always get the error relation "account" does not exist. What could be wrong?

  Caused by: org.postgresql.util.PSQLException: ERROR: relation "account" does not exist

CodePudding user response:

This exception is returned from the DB and converted into a java exception. In fact, you need to make sure that the table account exists in the DB.

CodePudding user response:

Finally I could fix it changing:

spring.jpa.hibernate.ddl-auto=create-drop

to

spring.jpa.hibernate.ddl-auto=update

in the application.properties file.

  • Related