Entity: product
@Entity
public class Product {
private String productNo;
private String name;
private String chandi;
private ProductType productType;
@Id
public String getProductNo() {
return productNo;
}
public void setProductNo(String productNo) {
this.productNo = productNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getChandi() {
return chandi;
}
public void setChandi(String chandi) {
this.chandi = chandi;
}
@ManyToOne(cascade ="CascadeType.All")
@JoinColumn(name="productType_id")
public ProductType getProductType() {
return productType;
}
public void setProductType(ProductType productType) {
this.productType = productType;
}
}
Entity class ProductType
@Entity
public class ProductType {
private String typeNo;
private String Name;
private Set<Product> products;
@Id
public String getTypeNo() {
return typeNo;
}
public void setTypeNo(String typeNo) {
this.typeNo = typeNo;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
@OneToMany(mappedBy="productType")
public Set<Product> getProducts() {
return products;
}
public void setProducts(Set<Product> products) {
this.products = products;
}
}
If the associated query: createQuery("select p.chandit.name from Product p left join p. productType t group by p. productType ") will return the object type. To process the entity, the following measures can be taken:
1. Add a class (the field name is the field name of the query, the corresponding geter seter method must be generated, and there must be a constructor, and the parameters of the constructor are the fields of the query)
public class ProductAndType {
private String name;
private String chandi;
public Product(String name, String chandi) {
this.name = name;
this.chandi = chandi;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getChandi() {
return chandi;
}
public void setChandi(String chandi) {
this.chandi = chandi;
}
}
2. Modify the query statement createQuery("select new package name. ProductAndType ( p.chandi t.name )from Product p left join p. productType t group by p. productType "), so that it can be converted to ProductAndType type, which can be operated on .