使用额外的列XML映射来休眠多对多

时间:2016-08-27 08:58:49

标签: java xml hibernate

我有很多mamy的问题,许多映射与额外的列。我不知道从表Recipe_Prduct获取数量值的内容。我应该改变什么?求助。

DB:

配方

  1. id_re,
  2. 名称

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

  3. Recipe_Product

    1. id_re,
    2. id_p,
    3. public class Recipe_Product implements Serializable{ private Recipe_Product id; private Recipe recipe; private Product product; private float quantity; }

    4. 产品

      1. id_p,
      2. 名称

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

      3. 映射

        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>
        

1 个答案:

答案 0 :(得分:3)

您好请查看以下代码&amp;映射,我能够使用下面的映射获得输出。

<强> 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 + "]";
}

映射文件: -

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);