Many-to-many example using Hibernate

Pertinent code for the two objects in the many-to-many relationship follows. Hibernate generates a third table, form_field_bridge, to bind together the relationship between multiple forms and fields.

FormField:


/**
* @return
* @hibernate.set inverse="false"
* lazy="false"
* cascade="save-update"
* table="form_field_bridge"
* @hibernate.collection-key column="field_id"
* @hibernate.collection-many-to-many class="org.zeprs.data.Form" column="form_id"
*/
public Set getForms()
{
return forms;
}

public void setForms(Set form)
{
this.forms = form;
}

Form:

/**
* @return
* @hibernate.set inverse="true"
* lazy="false"
* cascade="save-update"
* table="form_field_bridge"
* sort="org.zeprs.display.util.DisplayOrderComparator"
* @hibernate.collection-key column="form_id"
* @hibernate.collection-many-to-many class="org.zeprs.data.FormField" column="field_id"
*/
public Set getFields() {
return fields;
}

public void setFields(Set field) {
this.fields = field;
}

3 Responses to “Many-to-many example using Hibernate”

  1. Jeryl Cook Says:

    any reasons why a Form.getFields(), will not return collection of Fields? after a session.save ?? i am using spring/hibernate , thanks!

  2. Jeryl Cook Says:

    Do i use a formfieldbridge object also??..i generated the assocation table as well, but it has porblems using java2hbm

  3. iwan Says:

    Jeryl: try a session.refresh(Form) before retreiving the collection.