[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - trouble with bidirectional OneToMany and JoinColumn

bernieb do-not-reply at jboss.com
Sat Oct 7 01:09:58 EDT 2006


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.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3976720#3976720

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3976720



More information about the jboss-user mailing list