Sunday, November 6, 2011

Change Hibernate Mapping to Retrieve a List instead of a Set


Table 1- Employee
Table 2 - Destination
Relationship : Employee has multiple destinations
when you did the reverse engineering it will give the Set in Employee table.
Now we want to convert the set into list.so first of all we change the Set
object into the java.util.list object in the POJO file of employee
(which is made in dao folder when you make the reverse engineering )

Example: Set 
public class Employeeaddress implements java.io.Serializable {
private Set destinations = new HashSet(0);
/** full constructor */
public Employeeaddress( Set destinations){
    this.destinations = destinations;
    }

public Set getDestinations() {
return this.destinations;
}
public void setDestinations(Set destinations) {
this.destinations = destinations;
}
}

Convert into list :
Note: Change the set to list

  • private List destinations = new ArrayList();


public List getDestinations() {
return this.destinations;
}

public void setDestinations(List destinations) {
this.destinations = destinations;
}


  • 3- Change into the HBM file .


<list name="destinations" table="destinations">
    <key column="ID" />
    <list-index column="Location" />
   <one-to-many class="com.doa.Destination"/>
</list>


OR another example of List:

<list name="carComponents"
        table="CarComponents">
    <key column="carId"/>
    <list-index column="sortOrder"/>
    <composite-element class="CarComponent">
        <property name="price"/>
        <property name="type"/>
        <property name="serialNumber" column="serialNum"/>
    </composite-element>
</list>


For more information : http://docs.jboss.org/hibernate/core/3.3/reference/en/html/collections.html


No comments:

Post a Comment