Hibernate error Query defined multiple selections, return cannot be typed (other that Object[] or Tuple)

Viewed 45

I am new in Hibernate. And can not understand what is wrong. All what I did according examples. I need get left join result from 2 tables. First table is Product second table is Prod_Party. Relationship id(Product)to many id_prd(Prod_Party).

Main

    public static List<Product> findAllProductWithJpql(Session session) {
        return session.createQuery("SELECT a , b FROM Product a Left Join Prod_Party b ON a.id = b.id_prd",
                Product.class).getResultList();
    }

Product Class

import jakarta.persistence.*;

import java.util.ArrayList;
import java.util.List;


@Entity
@Table(name="Product")

public class Product {


    public Product(String  product_Name) {
        this.product_Name = product_Name;
    }
    public Product() {

    }
    @OneToMany(
            mappedBy = "product",
            cascade = CascadeType.ALL,
            orphanRemoval = true

    )
    private List<Prod_Party> prod_partyList= new ArrayList<>();

    public List<Prod_Party> getProd_partyList() {
        return prod_partyList;
    }

    public void setProd_partyList(List<Prod_Party> prod_partyList) {
        this.prod_partyList = prod_partyList;
    }

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }



    @Column(name="Product_Name")
    private String product_Name;

    public String getProduct_Name() {
        return product_Name;
    }

    public void setProduct_Name(String product_Name) {
        this.product_Name = product_Name;
    }
}


Prod_Party Class

import jakarta.persistence.*;
@Entity
@Table(name="Prod_Party")

public class Prod_Party {
        public Prod_Party(int  cost, int id_prd) {
            this.cost = cost;
            this.id_prd=id_prd;
        }
        public Prod_Party() {
        }

    @ManyToOne
    @JoinColumn(name="id_prd", referencedColumnName = "id", insertable = false, updatable = false)
    private Product product;

    public Product getProduct() {
        return product;
    }

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

    @Id
        @Column(name = "id")
            @GeneratedValue(strategy=GenerationType.IDENTITY)
        private int id;


    @Column(name="id_prd")
    private int id_prd;

    @Column(name="Cost")
    private int cost;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId_prd() {
        return id_prd;
    }

    public void setId_prd(int id_prd) {
        this.id_prd = id_prd;
    }

    public int getCost() {
        return cost;
    }

    public void setCost(int cost) {
        this.cost = cost;
    }

}



Exception in thread "main" java.lang.IllegalStateException: org.hibernate.query.IllegalQueryOperationException: Query defined multiple selections, return cannot be typed (other that Object[] or Tuple) at org.hibernate.query.spi.AbstractSelectionQuery.list(AbstractSelectionQuery.java:368) at org.hibernate.query.sqm.internal.QuerySqmImpl.list(QuerySqmImpl.java:987) at org.hibernate.query.Query.getResultList(Query.java:94) at Main.findAllProductWithJpql(Main.java:52) at Main.main(Main.java:27) Caused by: org.hibernate.query.IllegalQueryOperationException: Query defined multiple selections, return cannot be typed (other that Object[] or Tuple) at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.determineRowTransformer(ConcreteSqmSelectQueryPlan.java:209) at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.(ConcreteSqmSelectQueryPlan.java:87) at org.hibernate.query.sqm.internal.QuerySqmImpl.buildConcreteSelectQueryPlan(QuerySqmImpl.java:630) at org.hibernate.query.sqm.internal.QuerySqmImpl.buildSelectQueryPlan(QuerySqmImpl.java:602) at org.hibernate.query.internal.QueryInterpretationCacheStandardImpl.resolveSelectQueryPlan(QueryInterpretationCacheStandardImpl.java:83) at org.hibernate.query.sqm.internal.QuerySqmImpl.resolveSelectQueryPlan(QuerySqmImpl.java:582) at org.hibernate.query.sqm.internal.QuerySqmImpl.doList(QuerySqmImpl.java:537) at org.hibernate.query.spi.AbstractSelectionQuery.list(AbstractSelectionQuery.java:363) ... 4 more Thank you.

0 Answers
Related