在JPQL中使用new运算符来初始化构造函数

时间:2015-06-07 20:03:39

标签: hibernate jpa jpql

我正在尝试初始化一个名为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 gring_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")
    List<ShopOrder> getShopOrder();

它从名为partList的List中获取Po值,该列表位于此代码段的底部:

@Entity
@Table(name = "T_PO")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
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
    private List<Po_part> partList;

然而,我收到的一些错误让我感到有些困惑。它说我失踪了:

Caused by: org.hibernate.HibernateException: Missing table: T_PO_T_PO_PARŤ

Caused by: org.springframework.beans.factory.BeanCreationException: Error 
creating bean with name '(inner bean)#3e5970de': Cannot resolve reference to 
bean 'entityManagerFactory' while setting constructor argument; nested 
exception is org.springframework.beans.factory.BeanCreationException: Error 
creating bean with name 'entityManagerFactory' defined in class path 
resource: Invocation of init method failed; nested exception is   
org.hibernate.HibernateException: Missing table: T_PO_T_PO_PART

非常感谢任何建议。

我有一个Po_Part表,你可以告诉它是Po和Part的连接表。 Po和Part有一个ManyToMany关系,所以我对这个问题感到头疼。

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

这是我现在尝试做的,但现在仍在使用:

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

    @OneToMany
    List<Po> poList;

    @OneToMany
    List<Part>partList;

@Entity
@Table(name = "T_PO")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
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
    private List<Po_part> partList;

部分

@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
    private List<Po_part> partList;

但是现在我收到以下错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error 
creating bean with name '(inner bean)#6216f0e8': Cannot resolve reference to 
bean 'entityManagerFactory' while setting constructor argument; nested 
exception is org.springframework.beans.factory.BeanCreationException: Error 
creating bean with name 'entityManagerFactory' defined in class path 
resource: Missing table: T_PART_T_PO_PART

Picture of Database Structure

由于Po_part是连接表,但我需要来自Po和Part的信息来初始化Shop_order类,如:

@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")
    List<ShopOrder> getShopOrder(); 

我对如何做到这一点感到有点困惑。我仍在使用JPA和JavaEE。把这一切放在一起就是我遇到的问题。

我也试图像这样映射:

@OneToMany
    @JoinTable(name = "T_PO_PART",
    joinColumns = @JoinColumn(name="poId", referencedColumnName="po_id"),
    inverseJoinColumns = @JoinColumn(name="partId", referencedColumnName = "part_id"))
    List<Po> poList;

    @OneToMany
    @JoinTable(name = "T_PO_PART",
    joinColumns = @JoinColumn(name="partId", referencedColumnName="part_id"),
    inverseJoinColumns = @JoinColumn(name="poId", referencedColumnName = "po_id"))
    List<Part>partList; 

但是我遇到了以下错误:

Caused by: org.hibernate.MappingException: Unable to find column with 
logical name: part_id in org.hibernate.mapping.Table(T_PO) and its related 
supertables and secondary tables

我希望这篇文章对于发表这篇文章的人来说非常有用......

1 个答案:

答案 0 :(得分:0)

最后一次更新澄清了很多。暂时忘掉数据库。根据实体Po,Part和Po_part的代码,您有以下不同的关联,其中*和1是关联的起源/目的地的多重性:

Po (1)-------{partList}---->(*) Po_part

Po_part (1)--{poList}------>(*) Po

Po_part (*)--{po}---------->(1) Po

Po_part (1)--{part}-------->(*) Part

Po_part (*)--{partList}---->(1) Part

Part (1)-----{partList}---->(*) Po_part

所以你们之间总共有6种不同的关联。 这是你真正想要的吗?

基于您的数据库模型,我认为您正尝试在这些实体之间进行双向关联:

Po_part (1)<------>(*) Po

Po_part (1)<------>(*) Part

这是实现它的一种方式(注意 mappedBy 属性):

部分

@Entity
@Table(name = "T_PART")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Part implements Serializable {
...
@OneToMany(mappedBy="part")
private List<Po_part> partList;

@Entity
@Table(name = "T_PO")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Po implements Serializable {
...
@OneToMany(mappedBy="po")
private List<Po_part> partList;

Po_part

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

@ManyToOne
private Po po;

//-- DELETE the this associations --//
//@OneToMany
//List<Po> poList;
//@OneToMany
//List<Part>partList;

然后,如果您要将此关联映射到特定列名称或第三个表格,则必须将@JoinColumn / @JoinTable注释放在 po 和/或仅_Po_part_ entity的 part 属性(不要将其放在mappedBy属性中的注释中)。

我建议您阅读 Beginning Java EE 7(Apress)一书的第4章,以了解Java Persistence API的基础知识。接下来,在第5章 - 实体关系中,您将深入到关联和双向