I am trying to create a birectional OneToMany relationship.
When I attempt to populate a set of this relationship, the item gets inserted into the expected table, but its foreign key, which is supposed to get mapped to the owner of the item, is getting set as null instead.
Here's the code to the owner of the OneToMany relationship:
@Entity public class Owner implements Serializable { private long id; private Set<Item> items; public Owner(){ items = new HashSet<Item>(); } @Id @GeneratedValue(strategy=GenerationType.AUTO) public long getId(){ return id; } public void setId(long id){ this.id = id; } @OneToMany(targetEntity=Item.class, cascade={CascadeType.ALL}, fetch=FetchType.EAGER) @JoinColumn(name="id") public Set<Item> getItems(){ return items; } public void setItems(Set<Item> items){ this.items = items; } }
@Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="DISCRIMINATOR") public class Item implements Serializable { private Owner owner; private String description; @Id public String getDescription(){ return description; } public void setDescription(String description){ this.description = description; } @ManyToOne(targetEntity=Owner.class) @JoinColumn(name="id", updatable = false, insertable = false) public Owner getOwner(){ return owner; } public void setOwner(Owner owner){ this.owner = owner; } }