Я создал программное обеспечение на Spring MVC и сопоставил более 25 таблиц. Я использую только ленивую загрузку. Сейчас я борюсь с объединениями. Кто-нибудь может мне помочь? Я знаю, как сделать все соединения в обычном SQL, но HQL убивает меня. Вот два компонента:
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);
}
}
Это другой компонент:
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 + "]";
}
}
Я перебираю несколько коллекций, чтобы предотвратить отложенную загрузку. Я хочу заменить эту часть класса DAO на LEFT JOIN FETCH для всех таблиц ... Может кто-нибудь сказать мне, как это сделать? У меня есть тонны таблиц, и мой код просто не работает. Я получаю исключение приведения от Integer к объекту, хотя я явно запрашиваю создание списка: 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;
}
}