当多对一映射时,如何避免父实体的重复值

时间:2017-12-15 17:18:03

标签: java hibernate hibernate-mapping

我有一个类别“类别”& “产品”,他们有多对一的映射。每当我尝试在表中插入与第一行相同的类别下的第二行产品时。类别值重复,我不应该发生。因此,对于上述场景,我需要在产品表中将一个类别ID映射到两个产品。

package com.shop.model;

public class Product {

private int productId;
private String productName;
private String productDescription;
private int productPrice;
private ProductCategory productCat;
private int status;
private String date;

public int getProductId() {
    return productId;
}

public void setProductId(int productId) {
    this.productId = productId;
}

public String getProductName() {
    return productName;
}

public void setProductName(String productName) {
    this.productName = productName;
}

public String getProductDescription() {
    return productDescription;
}

public void setProductDescription(String productDescription) {
    this.productDescription = productDescription;
}

public int getProductPrice() {
    return productPrice;
}

public void setProductPrice(int productPrice) {
    this.productPrice = productPrice;
}

public ProductCategory getProductCat() {
    return productCat;
}

public void setProductCat(ProductCategory productCat) {
    this.productCat = productCat;
}

public int getStatus() {
    return status;
}

public void setStatus(int status) {
    this.status = status;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}
}


//category table 

package com.shop.model;

public class ProductCategory {

private int productCatId;
private String categoryName;
private String categoryDescription;

public int getProductCatId() {
    return productCatId;
}

public void setProductCatId(int productCatId) {
    this.productCatId = productCatId;
}

public String getCategoryName() {
    return categoryName;
}

public void setCategoryName(String categoryName) {
    this.categoryName = categoryName;
}

public String getCategoryDescription() {
    return categoryDescription;
}

public void setCategoryDescription(String categoryDescription) {
    this.categoryDescription = categoryDescription;
}
}


//Mapping xml files 

//product.mapping.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 
3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

<class name = "com.shop.model.Product" table="product">
    <id name = "productId">
        <generator class = "increment"/>
    </id>
    <property name = "productName"/>
    <property name = "productDescription"/>
    <property name = "productPrice"/>
    <property name = "status"/>
    <property name = "date"/>
    <many-to-one name = "productCat" class = 
"com.shop.model.ProductCategory"/>
</class>

</hibernate-mapping>

//category.mapping.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 
3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

 <class name = "com.shop.model.ProductCategory" table="productcategory">
    <id name = "productCatId">
        <generator class = "increment"/>
    </id>
    <property name = "categoryName"/>
    <property name = "categoryDescription"/>
</class>

</hibernate-mapping>


//Test class

package Test;

import com.shop.model.Product;
import com.shop.model.ProductCategory;
import com.shop.model.ProductComment;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Test {

public static void main(String[] args) {

    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory sf = cfg.buildSessionFactory();
    Session s = sf.openSession();
    Transaction t = s.beginTransaction();

    ProductCategory p = new ProductCategory();
    p.setCategoryName("Food");
    p.setCategoryDescription("Food items....");

    Product product = new Product();
    product.setProductName("Milk");
    product.setProductDescription("Fresh Milk");
    product.setProductPrice(200);
    product.setDate("11-10.2017");
    product.setStatus(1);
    product.setProductCat(p);

    ProductComment pcm = new ProductComment();
    pcm.setComment("Best product in the year");
    pcm.setTime("12-17.2017 1:20 PM");
    pcm.setProduct(product);

    ProductComment pcm2 = new ProductComment();
    pcm2.setComment("Peoples choise");
    pcm2.setTime("12-15-2017 3:20 PM");
    pcm2.setProduct(product);

    s.saveOrUpdate(p);
    s.saveOrUpdate(product);
    s.saveOrUpdate(pcm);
    s.saveOrUpdate(pcm2);

    t.commit();   
}
}

0 个答案:

没有答案