Mateusz Sobczak
Mateusz Sobczak

Reputation: 1623

Hibernate Many-to-Many with extra column XML mapping

I have problem with mamy to many mapping with extra column. I don't know what to get quantity value from table Recipe_Prduct. What should I change? Thx for help.

DB:

Recipe

  1. id_re,
  2. name

    public class Recipe implements Serializable{ private int id_re; private String name; private Set< Product > listOfProducts = new HashSet<>(); }

Recipe_Product

  1. id_re,
  2. id_p,
  3. quantity

    public class Recipe_Product implements Serializable{ private Recipe_Product id; private Recipe recipe; private Product product; private float quantity; }

Product

  1. id_p,
  2. name

    public class Product implements Serializable{ private int id_p; private String name; }

Mapping

Recipe.hbm.xml

<class name="Recipe" table="recipe">

    <id name="id_re" type="java.lang.Integer">
        <column name="id_re" />
        <generator class="identity" />
    </id>

    <property name="name" type="string">
        <column name="name" length="255" not-null="true" />
    </property>

    <set name="listOfProducts" table="recipe_product" lazy="false" fetch="select" cascade="all">
        <key>
            <column name="id_re" not-null="true" />
        </key>

        <many-to-many entity-name="com.packt.cookbook.domain.Product">
            <column name="id_p" not-null="true" />
        </many-to-many>
    </set>

</class>

Recipe_Product.hbm.xml

<class name="Recipe_Product" table="recipe_product">

    <composite-id name="id" class="Recipe_Product">
        <key-many-to-one name="recipe" class="Recipe" column="id_re" />
        <key-many-to-one name="product" class="Product" column="id_p" />
    </composite-id>

    <property name="quantity" type="float" column="quantity" />

</class>

Product.hbm.xml

<class name="Product" table="product">

    <id name="id_p" type="java.lang.Integer">
        <column name="id_p" />
        <generator class="identity" />
    </id>

    <property name="name" type="string">
        <column name="name" length="255" not-null="true" />
    </property>

</class>

Upvotes: 3

Views: 3441

Answers (1)

Nirav Shah
Nirav Shah

Reputation: 317

Hi Please find below code & Mapping, I am able to get the output using below mapping.

Product.java

private int productId;
private String name;
public int getProductId() {
    return productId;
}
public void setProductId(int productId) {
    this.productId = productId;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + productId;
    return result;
}
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Product other = (Product) obj;
    if (productId != other.productId)
        return false;
    return true;
}

Recipe.java

private int recipeId;
private String name;
private Set<ProductRecipe> productRecipe;
public int getRecipeId() {
    return recipeId;
}
public void setRecipeId(int recipeId) {
    this.recipeId = recipeId;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Set<ProductRecipe> getProductRecipe() {
    return productRecipe;
}

ProductRecipeMapping.java

private static final long serialVersionUID = -4466109438914540231L;
private Product product;
private Recipe recipe;
public Product getProduct() {
    return product;
}
public void setProduct(Product product) {
    this.product = product;
}
public Recipe getRecipe() {
    return recipe;
}
public void setRecipe(Recipe recipe) {
    this.recipe = recipe;
}
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((product == null) ? 0 : product.hashCode());
    result = prime * result + ((recipe == null) ? 0 : recipe.hashCode());
    return result;
}
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    ProductRecipeMapping other = (ProductRecipeMapping) obj;
    if (product == null) {
        if (other.product != null)
            return false;
    } else if (!product.equals(other.product))
        return false;
    if (recipe == null) {
        if (other.recipe != null)
            return false;
    } else if (!recipe.equals(other.recipe))
        return false;
    return true;
}

ProductRecipe

private static final long serialVersionUID = -673347532267697932L;
private ProductRecipeMapping id;
private int quntity;

public ProductRecipeMapping getId() {
    return id;
}
public void setId(ProductRecipeMapping id) {
    this.id = id;
}
public int getQuntity() {
    return quntity;
}
public void setQuntity(int quntity) {
    this.quntity = quntity;
}
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    return result;
}
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    ProductRecipe other = (ProductRecipe) obj;
    if (id == null) {
        if (other.id != null)
            return false;
    } else if (!id.equals(other.id))
        return false;
    return true;
}
@Override
public String toString() {
    return "ProductRecipe [quntity=" + quntity + "]";
}
public void setProductRecipe(Set<ProductRecipe> productRecipe) {
    this.productRecipe = productRecipe;
}
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + recipeId;
    return result;
}
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Recipe other = (Recipe) obj;
    if (recipeId != other.recipeId)
        return false;
    return true;
}
@Override
public String toString() {
    return "Recipe [recipeId=" + recipeId + ", name=" + name
            + ", productRecipe=" + productRecipe + "]";
}

Mapping Files:-

Product.hbm.xml

<class entity-name="product" name="Product" table="PRODUCT" batch-size="50" dynamic-update="true">
    <id name="productId" column="ID" type="java.lang.Integer" length="5">
        <generator class="identity" />
    </id>
    <property name="name" column="NAME" type="java.lang.String" not-null="true" length="6"></property>    
</class>

recipe.hbm.xml

<class entity-name="recipe" name="Recipe" table="RECIPE" batch-size="50" dynamic-update="true">
    <id name="recipeId" column="ID" type="java.lang.Integer" length="5">
        <generator class="identity" />
    </id>
    <property name="name" column="NAME" type="java.lang.String" not-null="true" length="6"></property>

    <set name="productRecipe" table="PRODUCT_RECIPE" lazy="true" access="field" fetch="select" cascade="all">
        <key>
            <column name="RECIPE_ID" not-null="true" />
        </key>
        <one-to-many entity-name="productRecipe"/>
    </set>
</class>

ProductRecipe.hbm.xml

<class entity-name="productRecipe" name="ProductRecipe" table="PRODUCT_RECIPE" batch-size="50" dynamic-update="true">
    <composite-id name="id" class="ProductRecipeMapping">
        <key-many-to-one name="recipe" entity-name="recipe" column="PRODUCT_ID" />
        <key-many-to-one name="product" entity-name="product" column="RECIPE_ID" />
    </composite-id>

       <property name="quntity" type="int" column="QUNTITY" /> 
</class>

Test.java

Recipe recipe = (Recipe) session.get("recipe",new Integer(1));
System.out.println(recipe);

Upvotes: 5

Related Questions