Home > database >  Why i am getting column not found error when doing bi directional mapping in jpa?
Why i am getting column not found error when doing bi directional mapping in jpa?

Time:05-11

I have two tables name Author(author_name, book_name, and version) as the primary key and Book with (book_name, version) as the primary key, i am trying to do bidirectional mapping in the following way

Author Table

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumns(value = {
    @JoinColumn(name = "book_name", nullable = false, insertable = false, updatable = false),
    @JoinColumn(name = "version", nullable = false, insertable = false, updatable = false)
    })
private Book courtbook;

Book Table

@OneToMany
            @JoinColumns(value = { @JoinColumn(name = "book_name", nullable = false, insertable = 
 false, updatable = false),
            @JoinColumn(name = "version", nullable = false, insertable = false, updatable = false), 
            @JoinColumn(name ="author_name", nullable = false, insertable = false, updatable = false)
    })
    private Author author;

when i am trying to fetch data i am getting the following error ERROR: column book0_.author_author_name does not exist

CodePudding user response:

update your existing source code as given below:

Author Table

@ManyToOne
private Book courtbook;

Book Table

@OneToMany(targetEntity = Book.class)
private List<Author> author;

CodePudding user response:

For a single relationship, it is usually mapped with a foreign key on one side that references the ID of the other, so you have to pick which that is. Since I suspect a book would have one author, while an author could write many books (and versions of that book), I'd have gone with a model more like:

@Entity
public class Author {
  @Id
  @Column(name="name")
  private String name;

  @OneToMany(mappedby="author")
  private List<Book> books;
}

@Entity
public class Book {
  @Id
  @GeneratedValue
  private Integer id;//make it easy and go with a single generated ID value

  @Column(name="name")
  private String name;
  
  private String version;

  @ManyToOne
  @JoinColumn(name ="author_name", referencedColumnName="name")
  private Author author;
}

Key is you need the join columns to define the foreign keys, and if you are to let JPA assign the values to those columns, they need to be writable. This puts the foreign key in the Book table, so that it gets a author_name column.

Alternatively, add a join table between the two so that a book can have many authors, and authors aren't limited to one book:

@Entity
public class Author {
  @Id
  @Column(name="name")
  private String name;

  @ManyToMany(mappedby="authors")
  private List<Book> books;
}

@Entity
public class Book {
  @Id
  @GeneratedValue
  private Integer id;//make it easy and go with a single generated ID value

  @Column(name="book_name")
  private String name;
  
  private String version;

  @ManyToMany
  @JoinTable(name = "book_authors", 
        joinColumns = @JoinColumn(name = "author_name"),
        inverseJoinColumns = @JoinColumn(name = "book_id"))
  private List<Author> authors;
}

If book has a composite primary key (name/version), you'll have to add join columns for both instead of a single 'book_id' in the relation table.

  • Related