unexpected token: part near line 1, column 155 [SELECT NEW

3.2k views Asked by At

I am getting an error and I can only assume there is something wrong with my query. I am trying to initialize a class with the "new" key word in JPQL like so:

@Query("SELECT NEW com.htd.domain.ShopOrder(po.id, po.po_number, "
            + "po.due_date, po_part.id, po_part.part_quantity, "
            + "part.id, part.part_number, part.part_description "
            + "part.plasma_hrs_per_part, part.grind_hrs_per_part, "
            + "part.mill_hrs_per_part, part.brakepress_hrs_per_part) "
            + "FROM Po po "
            + "LEFT JOIN po.partList po_part "
            + "LEFT JOIN po_part.part")
    List<ShopOrder> getShopOrder();

Po_part.java

@Entity
@Table(name = "T_PO_PART")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Po_part implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "part_quantity")
    private Integer part_quantity;

    @ManyToOne
    private Part part;

    @ManyToOne
    private Po po;

Po.java

@OneToMany(mappedBy="po",targetEntity=Po_part.class)
    private List<Po_part> poList;

part.java

@OneToMany(mappedBy="part",targetEntity=Po_part.class)
private List<Po_part> partList;

my database looks like:

enter image description here

ShopOrder.java

public ShopOrder(int po_id, long po_number, Date po_due_date,
long po_part_id, int part_quantity, int part_id, int  
part_number, String part_decription, long plasma_hrs, long gring_hours,
long mill_hrs, long breakpress_hrs) {

Error:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: part near line 1, column 155 [SELECT NEW com.htd.domain.ShopOrder(po.id, po.po_number, po.due_date, po_part.id, po_part.part_quantity, part.id, part.part_number, part.part_description part.plasma_hrs_per_part, part.grind_hrs_per_part, part.mill_hrs_per_part, part.brakepress_hrs_per_part) FROM com.htd.domain.Po po LEFT JOIN po.partList po_part LEFT JOIN po_part.part]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:304) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
    ... 72 common frames omitted

------------UPDATE--------------------

@Query("SELECT NEW com.htd.domain.ShopOrder(po.id, po.po_number, "
            + "po.due_date, po_part.id, po_part.part_quantity, "
            + "part.id, part.part_number, part.part_description, "
            + "part.plasma_hrs_per_part, part.grind_hrs_per_part, "
            + "part.mill_hrs_per_part, part.brakepress_hrs_per_part) "
            + "FROM Po po "
            + "LEFT JOIN po.partList po_part "
            + "LEFT JOIN po_part.part part "
            + "LEFT JOIN po_part.part")

Is still giving me the following error:

Invalid path: 'part.id' [SELECT NEW com.htd.domain.ShopOrder(po.id, 
po.po_numbe

--------------UPDATE----------------------

Part.java

@OneToMany(mappedBy="part",targetEntity=Po_part.class)
    private List<Po_part> partList;

Po.java

@OneToMany(mappedBy="po",targetEntity=Po_part.class)
    private List<Po_part> partList;


    public List<Po_part> getPartList() {
        return partList;
    }

Po_part.java

    @ManyToOne
    private Part part;

    @ManyToOne
    private Po po;

ShopOrder

public ShopOrder(int po_id, long po_number, Date po_due_date,
        long po_part_id, int part_quantity, int part_id, int part_number,
            String part_decription, long plasma_hrs, long grind_hours,
            long mill_hrs, long breakpress_hrs) {

query

@Query("SELECT NEW ShopOrder(po.id, po.po_number, "
            + "po.due_date, po_part.id, po_part.part_quantity, "
            + "part.id, part.part_number, part.part_description, "
            + "part.plasma_hrs_per_part, part.grind_hrs_per_part, "
            + "part.mill_hrs_per_part, part.brakepress_hrs_per_part) "
            + "FROM Po po "
            + "LEFT JOIN po.partList po_part "
            + "LEFT JOIN po_part.part part"
            + "LEFT JOIN po_part.part")
    List<ShopOrder> getShopOrder();

In your update you are passed object where as I as passing variables. Is this wrong? I am still getting the same error:

Invalid path: 'part.id' [SELECT NEW 

-----------UPDATE--------------

Po.java

public class Po implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "po_number")
    private String po_number;

    @Column(name = "sales_order_number")
    private String sales_order_number;

    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
    @JsonSerialize(using = CustomLocalDateSerializer.class)
    @JsonDeserialize(using = ISO8601LocalDateDeserializer.class)
    @Column(name = "due_date")
    private LocalDate due_date;

    @Column(name = "status")
    private String status;

    @Column(name = "total_sale", precision=10, scale=2)
    private BigDecimal total_sale;

    @ManyToOne
    @JoinColumn(name = "customer_id", referencedColumnName = "id")
    private Customer customer;

    @OneToMany(mappedBy="po",targetEntity=Po_part.class)
    private List<Po_part> partList;

part.java

@Entity
@Table(name = "T_PART")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Part implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "part_number")
    private String part_number;

    @Column(name = "part_description")
    private String part_description;

    @Column(name = "plasma_hrs_per_part", precision=12, scale=4)
    private BigDecimal plasma_hrs_per_part;

    @Column(name = "laser_hrs_per_part", precision=12, scale=4)
    private BigDecimal laser_hrs_per_part;

    @Column(name = "grind_hrs_per_part", precision=12, scale=4)
    private BigDecimal grind_hrs_per_part;

    @Column(name = "mill_hrs_per_part", precision=12, scale=4)
    private BigDecimal mill_hrs_per_part;

    @Column(name = "brakepress_hrs_per_part", precision=12, scale=4)
    private BigDecimal brakepress_hrs_per_part;

    @Column(name = "lb_per_part", precision=12, scale=4)
    private BigDecimal lb_per_part;

    @Column(name = "inventory_count")
    private Integer inventory_count;

    @ManyToMany
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    @JoinTable(name = "T_PART_MATERIAL",
               joinColumns = @JoinColumn(name="parts_id", referencedColumnName="ID"),
               inverseJoinColumns = @JoinColumn(name="materials_id", referencedColumnName="ID"))
    private Set<Material> materials = new HashSet<>();

    @OneToMany(mappedBy="part",targetEntity=Po_part.class)
    private List<Po_part> partList;


    public List<Po_part> getPartList() {
        return partList;
    }

Po_Part

public class Po_part implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "part_quantity")
    private Integer part_quantity;

    @ManyToOne
    private Part part;

    @ManyToOne
    private Po po;

ShopOrder

 public ShopOrder(int po_id, long po_number, Date po_due_date,
        long po_part_id, int part_quantity, int part_id, int part_number,
        String part_decription, long plasma_hrs, long grind_hours,
        long mill_hrs, long breakpress_hrs) {
2

There are 2 answers

7
Matthias H On BEST ANSWER

part.part_description part.plasma_hrs_per_part there is a colon missing

--UPDATE--

I made a simpler example that is running on my server.

@Entity
public class Po extends UUIDIdentified {
    private static final long serialVersionUID = 1L;
    @OneToMany(mappedBy = "po", targetEntity = PoPart.class)
    List<PoPart> poParts;

    public List<PoPart> getPoParts() {
        return poParts;
    }

    public void setPoParts(List<PoPart> poParts) {
        this.poParts = poParts;
    }
}

And

@Entity
public class Part extends UUIDIdentified {
    private static final long serialVersionUID = 1L;
    @OneToMany(mappedBy = "part", targetEntity = PoPart.class)
    List<PoPart> poParts;

    public List<PoPart> getPoParts() {
        return poParts;
    }

    public void setPoParts(List<PoPart> poParts) {
        this.poParts = poParts;
    }
}

And

@Entity
public class PoPart extends UUIDIdentified {
    private static final long serialVersionUID = 1L;

    @ManyToOne
    Po po;

    @ManyToOne
    Part part;

    public Po getPo() {
        return po;
    }

    public void setPo(Po po) {
        this.po = po;
    }
}

And

public class Poo {

    public Poo(Po po, PoPart poPart, Part part) {
        super();
        this.po = po;
        this.poPart = poPart;
        this.part = part;
    }

    Po po;

    PoPart poPart;

    Part part;

    public Po getPo() {
        return po;
    }

    public void setPo(Po po) {
        this.po = po;
    }

    public PoPart getPoPart() {
        return poPart;
    }

    public void setPoPart(PoPart poPart) {
        this.poPart = poPart;
    }

    public Part getPart() {
        return part;
    }

    public void setPart(Part part) {
        this.part = part;
    }

}

At Last my Query with correct Syntax:

<query><![CDATA[SELECT NEW de.cc.persistence.base.Poo(po, poParts, part) FROM Po po LEFT JOIN po.poParts poParts LEFT JOIN poParts.part part]]></query>
0
Tinh Cao On

Add part alias, you have many part.* but you don't have part alias.

LEFT JOIN po_part.part part