Java 使用额外的列XML映射休眠多对多
我对带有额外列的mamy-to-many映射有问题。我不知道如何从表中获取数量值。我应该换什么?谢谢你的帮助 DB: 配方Java 使用额外的列XML映射休眠多对多,java,xml,hibernate,Java,Xml,Hibernate,我对带有额外列的mamy-to-many映射有问题。我不知道如何从表中获取数量值。我应该换什么?谢谢你的帮助 DB: 配方 身份证 名字 公共类配方实现可序列化{ 私人国际身份证; 私有字符串名称; 私有集listOfProducts=newhashset(); } 配方产品 身份证 同上 数量 公共类Recipe\u产品实现可序列化{ 私有配方\产品id; 私家菜谱; 私人产品; 私人浮动量; } 产品 同上 名字 公共类产品实现可序列化{ 私人国际身份证; 私有字符串名称; } 映射 Rec
公共类配方实现可序列化{
私人国际身份证;
私有字符串名称;
私有集listOfProducts=newhashset();
}
公共类Recipe\u产品实现可序列化{
私有配方\产品id;
私家菜谱;
私人产品;
私人浮动量;
}
公共类产品实现可序列化{
私人国际身份证;
私有字符串名称;
}
Recipe_Product.hbm.xml
Product.hbm.xml
您好,请查找下面的代码和映射,我可以使用下面的映射获得输出 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;
}
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;
}
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;
}
Recipe.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;
}
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;
}
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;
}
产品配方
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 + "]";
}
这是ProductRecipe.hbm.xml列中的一个错误,应该是RECIPE_ID和PRODUCT_ID,而不是PRODUCT_ID-RECIPE_ID.THX。对于helpHi,您能否在问题中添加一个部分来解释需要更改哪些内容才能使其生效。我在接受的答案下看到了你的评论,但我无法理解。
<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>
<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>
<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>
Recipe recipe = (Recipe) session.get("recipe",new Integer(1));
System.out.println(recipe);