0

サブクエリを使用して CriteriaQuery を介して ProductCategory から製品の個別の配列を返す方法を知っている人はいますか? これらのチュートリアルを試しましたが、うまくいきませんでした:

CriteriaQuery を使用した where 句のサブクエリ

public String[] getProductsDistinctByName(String category) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Object[]> criteriaQuery = builder.createQuery(Object[].class);

    Subquery<Productscategory> subquery =criteriaQuery.subquery(Productscategory.class);
    Root productsCategory = criteriaQuery.from(Productscategory.class);
    subquery.select(productsCategory.<Integer>get(Productscategory_.productscategoryid))
           .where(builder.equal((productsCategory.<String>get(Productscategory_.productscategoryid)), category));

     Root<Products> root = criteriaQuery.from(Products.class);
    criteriaQuery.multiselect(root.get(Products_.productname)).distinct(true)
            .where(builder.in(root.get("productscategoryid")).value(subquery));

    List<Object[]> tupleResult = em.createQuery(criteriaQuery).getResultList();
    String[] arrayProducts = new String[tupleResult.size()];
    for (int i = 0; i < tupleResult.size(); i++) {
        arrayProducts[i] = (String) tupleResult.get(0).toString();
    }
    return arrayProducts;
}

public String[] getProductsDistinctBySubQueriesName(String category) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
    //subquery
    Subquery<Integer> subqueries = criteria.subquery(Integer.class);
    Root<Productscategory> productCategory = subqueries.from(Productscategory.class);
    subqueries.select(productCategory.<Integer>get(Productscategory_.productscategoryid))
            .where(builder.equal((productCategory.<String>get("productcatgoryname")), category));
    //outerquery
    Root<Products> root = criteria.from(Products.class);
    criteria.multiselect(root.get(Products_.productname)).distinct(true)
            .where(builder.in(root.get("productscategoryid")).value(subqueries));

    List<Tuple> tupleResult = em.createQuery(criteria).getResultList();
    String[] arrayProducts = new String[tupleResult.size()];
    for (int i = 0; i < tupleResult.size(); i++) {
        arrayProducts[i] = (String) tupleResult.get(i).get(0);
    }
    return arrayProducts;

}

エンティティはこちら

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;


@Entity
@Table(name = "productscategory")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Productscategory.findAll", query = "SELECT p FROM Productscategory p"),
@NamedQuery(name = "Productscategory.findByProductscategoryid", query = "SELECT p FROM Productscategory p WHERE p.productscategoryid = :productscategoryid"),
@NamedQuery(name = "Productscategory.findByProductcatgoryname", query = "SELECT p FROM Productscategory p WHERE p.productcatgoryname = :productcatgoryname"),
@NamedQuery(name = "Productscategory.findByProductcategorycomments", query = "SELECT p FROM Productscategory p WHERE p.productcategorycomments = :productcategorycomments"),
@NamedQuery(name = "Productscategory.findByStatusid", query = "SELECT p FROM Productscategory p WHERE p.statusid = :statusid")})

public class Productscategory implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "productscategoryid")
private Integer productscategoryid;
@Basic(optional = false)
@Column(name = "productcatgoryname")
private String productcatgoryname;
@Basic(optional = false)
@Column(name = "productcategorycomments")
private String productcategorycomments;
@Basic(optional = false)
@Column(name = "statusid")
private String statusid;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "productcategoryid")
private Collection<Priceitem> priceitemCollection;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "productcategoryid")
private Collection<Productssupplied> productssuppliedCollection;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "productscategoryid")
private Collection<Products> productsCollection;

public Productscategory() {
}

public Productscategory(Integer productscategoryid) {
    this.productscategoryid = productscategoryid;
}

public Productscategory(Integer productscategoryid, String productcatgoryname, String productcategorycomments, String statusid) {
    this.productscategoryid = productscategoryid;
    this.productcatgoryname = productcatgoryname;
    this.productcategorycomments = productcategorycomments;
    this.statusid = statusid;
}

public Integer getProductscategoryid() {
    return productscategoryid;
}

public void setProductscategoryid(Integer productscategoryid) {
    this.productscategoryid = productscategoryid;
}

public String getProductcatgoryname() {
    return productcatgoryname;
}

public void setProductcatgoryname(String productcatgoryname) {
    this.productcatgoryname = productcatgoryname;
}

public String getProductcategorycomments() {
    return productcategorycomments;
}

public void setProductcategorycomments(String productcategorycomments) {
    this.productcategorycomments = productcategorycomments;
}

public String getStatusid() {
    return statusid;
}

public void setStatusid(String statusid) {
    this.statusid = statusid;
}

@XmlTransient
public Collection<Priceitem> getPriceitemCollection() {
    return priceitemCollection;
}

public void setPriceitemCollection(Collection<Priceitem> priceitemCollection) {
    this.priceitemCollection = priceitemCollection;
}

@XmlTransient
public Collection<Productssupplied> getProductssuppliedCollection() {
    return productssuppliedCollection;
}

public void setProductssuppliedCollection(Collection<Productssupplied> productssuppliedCollection) {
    this.productssuppliedCollection = productssuppliedCollection;
}

@XmlTransient
public Collection<Products> getProductsCollection() {
    return productsCollection;
}

public void setProductsCollection(Collection<Products> productsCollection) {
    this.productsCollection = productsCollection;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (productscategoryid != null ? productscategoryid.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Productscategory)) {
        return false;
    }
    Productscategory other = (Productscategory) object;
    if ((this.productscategoryid == null && other.productscategoryid != null) || (this.productscategoryid != null && !this.productscategoryid.equals(other.productscategoryid))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "com.inventory.entity.Productscategory[ productscategoryid=" + productscategoryid + " ]";
}

}

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;



@Entity
@Table(name = "products")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Products.findAll", query = "SELECT p FROM Products p"),
@NamedQuery(name = "Products.findByProductid", query = "SELECT p FROM Products p WHERE p.productid = :productid"),
@NamedQuery(name = "Products.findByProductname", query = "SELECT p FROM Products p WHERE p.productname = :productname"),
@NamedQuery(name = "Products.findByMarketname", query = "SELECT p FROM Products p WHERE p.marketname = :marketname"),
@NamedQuery(name = "Products.findByModelvariant", query = "SELECT p FROM Products p WHERE p.modelvariant = :modelvariant"),
@NamedQuery(name = "Products.findByManufacturer", query = "SELECT p FROM Products p WHERE p.manufacturer = :manufacturer"),
@NamedQuery(name = "Products.findByProductdescription", query = "SELECT p FROM Products p WHERE p.productdescription = :productdescription"),
@NamedQuery(name = "Products.findByStatusid", query = "SELECT p FROM Products p WHERE p.statusid = :statusid")})

public class Products implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "productid")
private Integer productid;
@Basic(optional = false)
@Column(name = "productname")
private String productname;
@Basic(optional = false)
@Column(name = "marketname")
private String marketname;
@Basic(optional = false)
@Column(name = "modelvariant")
private String modelvariant;
@Basic(optional = false)
@Column(name = "manufacturer")
private String manufacturer;
@Column(name = "productdescription")
private String productdescription;
@Basic(optional = false)
@Column(name = "statusid")
private String statusid;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "productid")
private Collection<Productssupplied> productssuppliedCollection;
@JoinColumn(name = "productscategoryid", referencedColumnName = "productscategoryid")
@ManyToOne(optional = false)
private Productscategory productscategoryid;

public Products() {
}

public Products(Integer productid) {
    this.productid = productid;
}

public Products(Integer productid, String productname, String marketname, String modelvariant, String manufacturer, String statusid) {
    this.productid = productid;
    this.productname = productname;
    this.marketname = marketname;
    this.modelvariant = modelvariant;
    this.manufacturer = manufacturer;
    this.statusid = statusid;
}

public Integer getProductid() {
    return productid;
}

public void setProductid(Integer productid) {
    this.productid = productid;
}

public String getProductname() {
    return productname;
}

public void setProductname(String productname) {
    this.productname = productname;
}

public String getMarketname() {
    return marketname;
}

public void setMarketname(String marketname) {
    this.marketname = marketname;
}

public String getModelvariant() {
    return modelvariant;
}

public void setModelvariant(String modelvariant) {
    this.modelvariant = modelvariant;
}

public String getManufacturer() {
    return manufacturer;
}

public void setManufacturer(String manufacturer) {
    this.manufacturer = manufacturer;
}

public String getProductdescription() {
    return productdescription;
}

public void setProductdescription(String productdescription) {
    this.productdescription = productdescription;
}

public String getStatusid() {
    return statusid;
}

public void setStatusid(String statusid) {
    this.statusid = statusid;
}

@XmlTransient
public Collection<Productssupplied> getProductssuppliedCollection() {
    return productssuppliedCollection;
}

public void setProductssuppliedCollection(Collection<Productssupplied> productssuppliedCollection) {
    this.productssuppliedCollection = productssuppliedCollection;
}

public Productscategory getProductscategoryid() {
    return productscategoryid;
}

public void setProductscategoryid(Productscategory productscategoryid) {
    this.productscategoryid = productscategoryid;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (productid != null ? productid.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Products)) {
        return false;
    }
    Products other = (Products) object;
    if ((this.productid == null && other.productid != null) || (this.productid != null && !this.productid.equals(other.productid))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "com.inventory.entity.Products[ productid=" + productid + " ]";
}

}

4

0 に答える 0