从与JPA中的连接表的ManyToMany关系中获取ResultSet

时间:2015-05-24 18:58:14

标签: java mysql hibernate jpa

我有一个复杂的查询,我试图弄清楚如何将结果存储在模型中。例如:

public interface Po_partRepository extends JpaRepository<Po_part, Long> {

    @Query(value = "SELECT * FROM T_PO_PART u WHERE u.po_id LIKE %?1", 
  nativeQuery = true)
    List<Po_part> findByPoId(Long poId);

    @Query(value = "SELECT " + "po.id po_id," + "po.po_number po_number,"
            + "po.due_date po_due_date," + "po_part.id po_part_id,"
            + "po_part.part_quantity part_quantity," + "part.id part_id,"
            + "part.id part_id," + "part.part_number part_number,"
            + "part.part_description part_description,"
            + "part.plasma_hrs_per_part plasma_hrs,"
            + "part.grind_hrs_per_part grind_hrs,"
            + "part.mill_hrs_per_part mill_hrs,"
            + "part.brakepress_hrs_per_part brakepress_hrs"
            + "FROM hillcresttooldie.t_po po "
            + "join hillcresttooldie.t_po_part po_part "
            + "on po_part.po_id = po.id " + "join hillcresttooldie.t_part part "
            + "on part.id = po_part.part_id;", nativeQuery = true)
    List<Shop_Orders> getShopOrders();

Shop_Orders类只是一个简单的模型:

public class Shop_Orders {

    private int po_id;
    private int po_number;
    private Date po_due_date;
    private int part_quanity;
    private int part_id;
    private String part_description;
    private int plasma_hrs;
    private int grind_hrs;
    private int mill_hrs;
    private int breakpress_hrs;

    public int getPo_id() {
        return po_id;
    }

    public void setPo_id(int po_id) {
        this.po_id = po_id;
    }

    public int getPo_number() {
        return po_number;
    }

以下是此查询所依据的Po.java类和Part.java类的片段。

/**
 * A 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;

part.java

/**
 * A Part.
 */
@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 = "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<>();

现在,当我运行此查询时,我收到两个错误:

Exception in com.htd.web.rest.Po_partResource.getAll() with cause = 
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
org.springframework.dao.InvalidDataAccessResourceUsageException: could not    
extract ResultSet; SQL [n/a]; nested exception is 
org.hibernate.exception.SQLGrammarException: could not extract ResultSet

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You 
have an error in your SQL syntax; check the manual that corresponds to your 
MySQL server version for the right syntax to use near 'hillcresttooldie.t_po 
po join hillcresttooldie.t_po_part po_part on po_part.po_i' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)   
~[na:1.8.0_31]

查询在mySQL工作台中工作,而不是在JPA中。我知道JPA的语法有点不同,但我认为nativeQuery支持纯SQL语法。

到目前为止和ResultSet问题我不确定任何mappedBy声明,因为我使用了@JoinTable。

我很清楚我的两个问题是: 1)从我上面的查询中,我该如何设置我的模型? 2)nativeQuery确实允许纯SQL语法吗?

1 个答案:

答案 0 :(得分:0)

由于您的po_part表不仅仅是一个简单的连接表(它具有订单中每个部分的数量),因此您需要将此连接表映射到单独的实体(称为{{1} },说)。此实体将与Po_PartPo都有关联,并且该订单上的零件数量字段。

然后,您的Part实体将拥有订单中各个部分的集合,假设Po有一个Po_Part引用回订单:

po

有关查询详情,请参阅我的answer to your related question