首页 > 解决方案 > 在 Hibernate 上加入多个不同的表

问题描述

我在 Spring MVC 上创建了一个软件并映射了超过 25 个表。我只使用延迟加载。现在我正在为连接而苦苦挣扎。有人能帮我吗?我知道如何在常规 SQL 中进行所有连接,但 HQL 正在杀死我。这是两个Bean:

package dao;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.Hibernate;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.UpdateTimestamp;

@Entity()
@Table(name = "ix_products")
public class ProductIx {

    @Id()
    @GeneratedValue
    @Column(name = "ix_idproducts")
    private int productId;

    @Column(name = "ix_product_IAN")
    private String productIan;

    @Column(name = "ix_product_EAN")
    private String productEan;

    @Column(name = "ix_product_UPC")
    private String productUpc;

    @Column(name = "ix_product_barcode")
    private String productBarcode;

    @CreationTimestamp
    @Column(name = "ix_product_timestamp_create")
    private Timestamp productCreationTimestamp;

    @UpdateTimestamp
    @Column(name = "ix_product_timestamp_update")
    private Timestamp productUpdateTimestamp;

    @Fetch(FetchMode.JOIN)
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "productLinkImage", fetch = FetchType.LAZY)
    private List<ProductIxImage> productImages = new ArrayList<ProductIxImage>();

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "productLinkMeta", fetch = FetchType.LAZY)
    private ProductIxMetaDe productMeta;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "productLinkStatus", fetch = FetchType.LAZY)
    private ProductIxStatus productStatus;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "productLinkSingleShippingProduct", fetch = FetchType.LAZY)
    private ProductIxShippingSingleProduct productSingleShipping;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "productLinkShippingPackage", fetch = FetchType.LAZY)
    private ProductIxShippingPackage productShippingPackage;

    @ManyToMany(mappedBy = "productsInTags", fetch = FetchType.LAZY)
    private List<ProductIxTag> productTags = new ArrayList<ProductIxTag>();

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "productLinkAttributes", fetch = FetchType.LAZY)
    private ProductIxAttributes productAttribute;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "productLinkNutrition", fetch = FetchType.LAZY)
    private ProductIxNutrition productNutrition;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "productLinkManufacturer", fetch = FetchType.LAZY)
    private List<CmsManufacturer> productManufacturers = new ArrayList<CmsManufacturer>();

    @ManyToMany(mappedBy = "productsInCountries", fetch = FetchType.LAZY)
    private List<CmsCountryDe> productCountries = new ArrayList<CmsCountryDe>();

    @ManyToMany(mappedBy = "productInCategories", fetch = FetchType.LAZY)
    private List<ProductIxCategoryDe> productCategories = new ArrayList<ProductIxCategoryDe>();

    // Constructor for Spring
    public ProductIx() {
        // TODO Auto-generated constructor stub
    }

    // Constructor for J-Unit Tests
    public ProductIx(int productId, String productIan, String productEan, String productUpc, String productBarcode) {
        super();
        this.productId = productId;
        this.productIan = productIan;
        this.productEan = productEan;
        this.productUpc = productUpc;
        this.productBarcode = productBarcode;
    }

    public int getProductId() {
        return productId;
    }

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

    public String getProductIan() {
        return productIan;
    }

    public void setProductIan(String productIan) {
        this.productIan = productIan;
    }

    public String getProductEan() {
        return productEan;
    }

    public void setProductEan(String productEan) {
        this.productEan = productEan;
    }

    public String getProductUpc() {
        return productUpc;
    }

    public void setProductUpc(String productUpc) {
        this.productUpc = productUpc;
    }

    public String getProductBarcode() {
        return productBarcode;
    }

    public void setProductBarcode(String productBarcode) {
        this.productBarcode = productBarcode;
    }

    public Timestamp getProductCreationTimestamp() {
        return productCreationTimestamp;
    }

    public void setProductCreationTimestamp(Timestamp productCreationTimestamp) {
        this.productCreationTimestamp = productCreationTimestamp;
    }

    public Timestamp getProductUpdateTimestamp() {
        return productUpdateTimestamp;
    }

    public void setProductUpdateTimestamp(Timestamp productUpdateTimestamp) {
        this.productUpdateTimestamp = productUpdateTimestamp;
    }

    public List<ProductIxImage> getProductImages() {
        return productImages;
    }

    public void setProductImages(List<ProductIxImage> productImages) {
        this.productImages = productImages;
    }

    public ProductIxMetaDe getProductMeta() {
        return productMeta;
    }

    public void setProductMeta(ProductIxMetaDe productMeta) {
        this.productMeta = productMeta;
    }

    public ProductIxStatus getProductStatus() {
        return productStatus;
    }

    public void setProductStatus(ProductIxStatus productStatus) {
        this.productStatus = productStatus;
    }

    public ProductIxShippingSingleProduct getProductSingleShipping() {
        return productSingleShipping;
    }

    public void setProductSingleShipping(ProductIxShippingSingleProduct productSingleShipping) {
        this.productSingleShipping = productSingleShipping;
    }

    public ProductIxShippingPackage getProductShippingPackage() {
        return productShippingPackage;
    }

    public void setProductShippingPackage(ProductIxShippingPackage productShippingPackage) {
        this.productShippingPackage = productShippingPackage;
    }

    public List<ProductIxTag> getProductTags() {
        return productTags;
    }

    public void setProductTags(List<ProductIxTag> productTags) {
        this.productTags = productTags;
    }

    public ProductIxAttributes getProductAttribute() {
        return productAttribute;
    }

    public void setProductAttribute(ProductIxAttributes productAttribute) {
        this.productAttribute = productAttribute;
    }

    public ProductIxNutrition getProductNutrition() {
        return productNutrition;
    }

    public void setProductNutrition(ProductIxNutrition productNutrition) {
        this.productNutrition = productNutrition;
    }

    public List<CmsManufacturer> getProductManufacturers() {
        return productManufacturers;
    }

    public void setProductManufacturers(List<CmsManufacturer> productManufacturers) {
        this.productManufacturers = productManufacturers;
    }

    public List<CmsCountryDe> getProductCountries() {
        return productCountries;
    }

    public void setProductCountries(List<CmsCountryDe> productCountries) {
        this.productCountries = productCountries;
    }

    public List<ProductIxCategoryDe> getProductCategories() {
        return productCategories;
    }

    public void setProductCategories(List<ProductIxCategoryDe> productCategories) {
        this.productCategories = productCategories;
    }

    public boolean add(ProductIxImage e) {
        return productImages.add(e);
    }

    public void add(int index, ProductIxImage element) {
        productImages.add(index, element);
    }

    public boolean add(ProductIxTag e) {
        return productTags.add(e);
    }

    public void add(int index, ProductIxTag element) {
        productTags.add(index, element);
    }

    public boolean add(CmsManufacturer e) {
        return productManufacturers.add(e);
    }

    public void add(int index, CmsManufacturer element) {
        productManufacturers.add(index, element);
    }

    public boolean add(CmsCountryDe e) {
        return productCountries.add(e);
    }

    public void add(int index, CmsCountryDe element) {
        productCountries.add(index, element);
    }

    public boolean add(ProductIxCategoryDe e) {
        return productCategories.add(e);
    }

    public void add(int index, ProductIxCategoryDe element) {
        productCategories.add(index, element);
    }

}

这是另一个 bean:


package dao;

import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

@Entity
@Table(name = "ix_product_images")
public class ProductIxImage {

    @Id()
    @GeneratedValue()
    @Column(name = "ix_idproduct_image")
    private int productIdImage;

    @Column(name = "ix_product_image_title")
    private String ProductImageTitle;

    @Column(name = "ix_product_image_url")
    private String productImageUrl;

    @Column(name = "ix_product_image_description")
    private String productImageDescription;

    @Column(name = "ix_product_image_alternative")
    private String productImageAlternative;

    @Column(name = "ix_product_is_main_image")
    private boolean isMainImage;

    @Column(name = "ix_product_image_type")
    private String productImageType;

    @CreationTimestamp
    @Column(name = "ix_product_images_timestamp_create")
    private Timestamp productImageCreationTimestamp;

    @UpdateTimestamp
    @Column(name = "ix_product_images_timestamp_update")
    private Timestamp productImageUpdateTimestamp;

    @ManyToOne
    @JoinColumn(name = "ix_idproducts_fk_image")
    private ProductIx productLinkImage;

    // Constructor for Spring
    public ProductIxImage() {
        this.productLinkImage = new ProductIx();

    }

    // Constructor for Junit Tests
    public ProductIxImage(int productIdImage, String productImageTitle, String productImageUrl,
            String productImageDescription, String productImageAlternative, boolean isMainImage,
            String productImageType, ProductIx productLinkImage) {
        super();
        this.productIdImage = productIdImage;
        ProductImageTitle = productImageTitle;
        this.productImageUrl = productImageUrl;
        this.productImageDescription = productImageDescription;
        this.productImageAlternative = productImageAlternative;
        this.isMainImage = isMainImage;
        this.productImageType = productImageType;
        this.productLinkImage = productLinkImage;
    }

    public int getProductIdImage() {
        return productIdImage;
    }

    public void setProductIdImage(int productIdImage) {
        this.productIdImage = productIdImage;
    }

    public String getProductImageTitle() {
        return ProductImageTitle;
    }

    public void setProductImageTitle(String productImageTitle) {
        ProductImageTitle = productImageTitle;
    }

    public String getProductImageUrl() {
        return productImageUrl;
    }

    public void setProductImageUrl(String productImageUrl) {
        this.productImageUrl = productImageUrl;
    }

    public String getProductImageDescription() {
        return productImageDescription;
    }

    public void setProductImageDescription(String productImageDescription) {
        this.productImageDescription = productImageDescription;
    }

    public String getProductImageAlternative() {
        return productImageAlternative;
    }

    public void setProductImageAlternative(String productImageAlternative) {
        this.productImageAlternative = productImageAlternative;
    }

    public boolean isMainImage() {
        return isMainImage;
    }

    public void setMainImage(boolean isMainImage) {
        this.isMainImage = isMainImage;
    }

    public String getProductImageType() {
        return productImageType;
    }

    public void setProductImageType(String productImageType) {
        this.productImageType = productImageType;
    }

    public Timestamp getProductImageCreationTimestamp() {
        return productImageCreationTimestamp;
    }

    public void setProductImageCreationTimestamp(Timestamp productImageCreationTimestamp) {
        this.productImageCreationTimestamp = productImageCreationTimestamp;
    }

    public Timestamp getProductImageUpdateTimestamp() {
        return productImageUpdateTimestamp;
    }

    public void setProductImageUpdateTimestamp(Timestamp productImageUpdateTimestamp) {
        this.productImageUpdateTimestamp = productImageUpdateTimestamp;
    }

    public ProductIx getProduct() {
        return productLinkImage;
    }

    public void setProduct(ProductIx product) {
        this.productLinkImage = product;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((ProductImageTitle == null) ? 0 : ProductImageTitle.hashCode());
        result = prime * result + (isMainImage ? 1231 : 1237);
        result = prime * result + ((productLinkImage == null) ? 0 : productLinkImage.hashCode());
        result = prime * result + productIdImage;
        result = prime * result + ((productImageAlternative == null) ? 0 : productImageAlternative.hashCode());
        result = prime * result
                + ((productImageCreationTimestamp == null) ? 0 : productImageCreationTimestamp.hashCode());
        result = prime * result + ((productImageDescription == null) ? 0 : productImageDescription.hashCode());
        result = prime * result + ((productImageType == null) ? 0 : productImageType.hashCode());
        result = prime * result + ((productImageUpdateTimestamp == null) ? 0 : productImageUpdateTimestamp.hashCode());
        result = prime * result + ((productImageUrl == null) ? 0 : productImageUrl.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;
        ProductIxImage other = (ProductIxImage) obj;
        if (ProductImageTitle == null) {
            if (other.ProductImageTitle != null)
                return false;
        } else if (!ProductImageTitle.equals(other.ProductImageTitle))
            return false;
        if (isMainImage != other.isMainImage)
            return false;
        if (productLinkImage == null) {
            if (other.productLinkImage != null)
                return false;
        } else if (!productLinkImage.equals(other.productLinkImage))
            return false;
        if (productIdImage != other.productIdImage)
            return false;
        if (productImageAlternative == null) {
            if (other.productImageAlternative != null)
                return false;
        } else if (!productImageAlternative.equals(other.productImageAlternative))
            return false;
        if (productImageCreationTimestamp == null) {
            if (other.productImageCreationTimestamp != null)
                return false;
        } else if (!productImageCreationTimestamp.equals(other.productImageCreationTimestamp))
            return false;
        if (productImageDescription == null) {
            if (other.productImageDescription != null)
                return false;
        } else if (!productImageDescription.equals(other.productImageDescription))
            return false;
        if (productImageType == null) {
            if (other.productImageType != null)
                return false;
        } else if (!productImageType.equals(other.productImageType))
            return false;
        if (productImageUpdateTimestamp == null) {
            if (other.productImageUpdateTimestamp != null)
                return false;
        } else if (!productImageUpdateTimestamp.equals(other.productImageUpdateTimestamp))
            return false;
        if (productImageUrl == null) {
            if (other.productImageUrl != null)
                return false;
        } else if (!productImageUrl.equals(other.productImageUrl))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "ProductIxImage [productIdImage=" + productIdImage + ", ProductImageTitle=" + ProductImageTitle
                + ", productImageUrl=" + productImageUrl + ", productImageDescription=" + productImageDescription
                + ", productImageAlternative=" + productImageAlternative + ", isMainImage=" + isMainImage
                + ", productImageType=" + productImageType + ", productImageCreationTimestamp="
                + productImageCreationTimestamp + ", productImageUpdateTimestamp=" + productImageUpdateTimestamp
                + ", product=" + productLinkImage + "]";
    }

}

我遍历一堆集合以防止延迟加载。我想用所有表的 LEFT JOIN FETCH 替换 DAO 类的这一部分...有人可以告诉我该怎么做吗?我有很多表,我的代码似乎不起作用。我得到一个从整数到对象的转换异常,即使我清楚地查询创建一个列表:S...映射是正确的,其余的也很好......

@Repository
@Transactional
@Component("productIxDao")
public class ProductIxDao {

    public ProductIxDao() {
    }

    @Autowired
    SessionFactory sessionFactory;

    Session session;

    @SuppressWarnings("unchecked")
    public List<ProductIx> getAllProducts() {

        session = sessionFactory.getCurrentSession();
        List<ProductIx> tempListProducts = session.createQuery("from ProductIx").list();

        /* Initialize to prevent lazy loading exception
        List<ProductIxCategoryDe> tempListCategoriesDe = session.createQuery("from ProductIxCategoryDe").list();
        List<ProductIxTag> tempListTags = session.createQuery("from ProductIxTag").list();
        List<CmsCountryDe> tempListCountries = session.createQuery("from CmsCountryDe").list();

        for (int i = 0; i < tempListProducts.size(); i++) {
            tempListProducts.get(i).getProductImages().size();
            tempListProducts.get(i).getProductTags().size();
            tempListProducts.get(i).getProductManufacturers().size();
            tempListProducts.get(i).getProductCountries().size();
            tempListProducts.get(i).getProductCategories().size();
        }

        for (int i = 0; i < tempListCategoriesDe.size(); i++) {
            tempListCategoriesDe.get(i).getProductCategoriesChildren().size();
            tempListCategoriesDe.get(i).getProductCategoriesParents().size();
            tempListCategoriesDe.get(i).getProductInCategories().size();
        }

        for (int i = 0; i < tempListTags.size(); i++) {
            tempListTags.get(i).getProductsInTags().size();
        }

        for (int i = 0; i < tempListCountries.size(); i++) {
            tempListCountries.get(i).getProductsInCountries().size();
        }
         */

        //HOW DO I JOIN TWO TABLES?

        List<ProductIx> entireDb = session.createQuery(
        "SELECT DISTINCT product.productId "
        + "FROM ProductIx product "
        + "LEFT JOIN FETCH ProductIxImage img "
        + "ON product.productId=img.productLinkImage.productId "
        + "", ProductIx.class).getResultList();

        return entireDb;
    }

}

标签: javaspringhibernatemappinghql

解决方案


推荐阅读