意外标记:第1行第155列附近[SELECT NEW

时间:2015-06-10 17:30:09

标签: java jpa jpql

我收到错误,我只能假设我的查询有问题。我正在尝试使用" new"来初始化一个类。 JPQL中的关键词如下:

@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;

我的数据库看起来像:

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) {

错误:

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")

仍然给我以下错误:

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("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();

在您的更新中,您将传递对象,就像传递变量一样。这是错的吗?我仍然得到同样的错误:

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 个答案:

答案 0 :(得分:1)

part.part_description part.plasma_hrs_per_part缺少冒号

- UPDATE -

我做了一个在我的服务器上运行的更简单的例子。

@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;
    }
}

@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;
    }
}

并且

@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;
    }
}

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;
    }

}

使用正确的语法在最后一次查询:

<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>

答案 1 :(得分:1)

添加别名,你有很多部分。*但你没有别名。

LEFT JOIN po_part.part part