0 Replies Latest reply on Oct 7, 2006 1:09 AM by Brian Bernstein

    trouble with bidirectional OneToMany and JoinColumn

    Brian Bernstein Newbie

      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;
       }
      }
      


      Here's the code to the Item class, the other end of the OneToMany relationship:
      @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;
       }
      }
      


      Item also happens to contain inherited classes (stored with the single column and descriminator method), of which can be included in the set in owner. Also, description is a manually assigned primary key for items.

      When an item is added to the set of an owner, a row is inserted into the Item table, but does not get associated to its owner-- the joined column 'id' remain null on the item's end.

      Am I making an error with regard to the JoinColumn statements? Or am I doing something else wrong? Any help will be greatly appreciated. Thanks in advance for all your help.