Composition as set of many classes

Full source code is provided in the package: de.laliluna.component.collection2




Annotation mapping. 

import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
....... snip ........
public class Pizza {
// ... snip ...

   @CollectionTable(name="pizza_ingredients", joinColumns =
   private Set<Ingredient> ingredients = new HashSet<Ingredient>();

@ElementCollection defines the component mapping. @JoinTable is optional. It specifies the name of the ingredient table and the foreign key column.

Deprecated Hibernate extension

Before Java Persistence 2 only Hibernate supported this kind of mapping. The annotation was named slightly different @CollectionOfElements and is now deprecated.

import org.hibernate.annotations.Parent;
import javax.persistence.Embeddable;

public class Ingredient {

   private String name;

   private Pizza client;

@Parent specifies the property to be a reference back to the embedding class, i.e. PizzaClient in our case.

XML mapping. 

<hibernate-mapping package="de.laliluna.component.collection2">
<class name="Pizza" table="tclient" >
  <!-- .. snip .. ->
  <set name="ingredients" table="pizza_ingredients">
    <key column="client_fk"></key>
    <composite-element class="de.laliluna.component.collection2.Ingredient"  >
      <property name="name"/>

Samples of use:

/* create and set component */
Pizza pizza = new Pizza("Speciale");
Ingredient cheese = new Ingredient("Cheese");
Ingredient salami = new Ingredient("Salami");
Ingredient tomatoes = new Ingredient("Tomatoes");

/* select pizza clients having an address in London */
List<PizzaClient> list = session
   "from Pizza c left join c.ingredients a where = :ingr")
   .setString("ingr", "Tomato").list();