I'm trying to save an item in the database which has manytoone relation with the Librarian(which is a subclass of user). The code below creates a table *librarian_item* which has librarian_id and item_id as attributes. When I try to save the data, the program prints no *librarian_id*. The librarian is set on the item but still it throws error.
@ManyToOne(cascade=CascadeType.PERSIST, fetch = FetchType.LAZY)
@JoinTable(name = "librarian_item",
joinColumns = @JoinColumn(name = "item_id"),
inverseJoinColumns = @JoinColumn(name = "librarian_id"))
@NotNull
@Valid
private User librarian;
The Item class has following code for Hibernate:
@OneToMany(cascade=CascadeType.PERSIST)
@JoinTable(name = "librarian_item",
joinColumns = @JoinColumn(name="librarian_id"),
inverseJoinColumns = @JoinColumn(name="item_id"))
@Valid
public Collection<Item> getItemList() {
return itemList;
}
As Librarian is a subclass of User and I have used discriminator to identify each type of User in the user table, *librarian_id* is necessarily a *user_id*.
I changed my code as it is discussed here. Apparently, the new approach doesn't create a new table. A new column is created in the table which references to another table to establish a relationship.