[forge-dev] [Proposal] Helper methods for managing inverse relationships

Marius Bogoevici mariusb at redhat.com
Mon Sep 24 10:37:16 EDT 2012


All,

I have a proposal for the persistence plugin, regarding the handling of inverse relationships.

When working with inverse relationships in Hibernate and JPA, it is a common and encouraged practice to include convenience methods for correctly managing the inverse end of the relationship, so that the entity user would be relieved from the burden of setting each end manually when the relationship. e.g. 'addSubCategory' in

@Entity
public class SubCategory {

 @ManyToOne
private Category parent;
}


@Entity
public class Category {

  @OneToMany(mappedBy = "parent")
  private Set<SubCategory> subcategories = new HashSet<SubCategory>();

  ….


  public void addSubCategory(SubCategory subcategory) {
	this.subcategories.add(subcategory);
	subcategory.setParent(this);
  }

}

or something more elaborate (null checks etc) but to the effect of . This can be combined with returning non-modifiable collections by the getters, etc. but the gist of it is to provide a single operation through which developers will set up the bi-directional relationship, thus preventing the model from becoming inconsistent (or even not persisted correction).

Essentially this is a quite established good practice, which the ones of us who admit to being old enough to have read Gavin&Christian's book will remember. Now this is a PITA to do manually, but Forge could easily take care of it.

Lincoln and I have discussed this on IRC, and whiles this has been deemed a good idea,  we're wondering what everyone else thinks. So, thoughts?

Cheers,
Marius




More information about the forge-dev mailing list