我只是尝试通过JPA查询初始化列表中的对象。到目前为止,我在1个半天的行程中没有成功。
public interface Po_partRepository extends JpaRepository<Po_part, Long> {
@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.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模型
@Entity
@Table(name = "SHOP_ORDERS")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Shop_Orders implements Serializable {
/**
*
*/
private static final long serialVersionUID = 799464578815300087L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int shop_order_id;
@Column(name = "po_id")
private int po_id;
@Column(name = "po_number")
private int po_number;
@Column(name = "po_date")
private Date po_due_date;
@Column(name = "part_quanity")
private int part_quanity;
@Column(name = "part_id")
private int part_id;
@Column(name = "part_description")
private String part_description;
@Column(name = "plasma_hrs")
private int plasma_hrs;
@Column(name = "grind_hrs")
private int grind_hrs;
@Column(name = "mill_hrs")
private int mill_hrs;
@Column(name = "breakpress_hrs")
private int breakpress_hrs;
public Shop_Orders() {
}
public int getPo_id() {
return po_id;
}
我为shop_orders创建了表:
以下是运行查询并在列表中设置对象后我想要执行的操作的示例。
Shop_Orders shopOrders;
int poNumber = shopOrders.getPo_number();
但是,当我运行查询时,我一直收到以下错误
[ERROR] com.htd.aop.logging.LoggingAspect - Exception in
com.htd.web.rest.Po_partResource.getAll() with cause = null
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to
com.htd.repository.Shop_Orders
at com.htd.web.rest.Po_partResource.getAll(Po_partResource.java:85) ~
[classes/:na]
我会发布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;
提前感谢您的建议。
答案 0 :(得分:1)
如果您的表格中包含与您的Shop_orders
实体相对应的数据,那么您的查询应该是:
@Query("SELECT so FROM Shop_Orders so")
由于您已将Shop_Orders
实体映射到该表。
但是,由于您似乎正在构建所涉及的3个表的反规范化模型(PO,Part和两者之间的连接表,PO_Part),那么您可能需要为此创建一个非规范化的数据库VIEW。您拥有的原始查询,然后通过在Shop_Orders
注释中指定该视图,将该视图映射到您的@Table
实体。
或者,您可以为Shop_Orders
创建一个构造函数(不需要是JPA实体),它可以获取所有列。换句话说:
public Shop_Orders(int po_id, int po_number, Date po_due_date, etc... )
然后,假设您有Po_Part
和Po
之间的联接表的实体Part
(请参阅my answer to your other question),查询可以是:
@Query("SELECT new Shop_Orders(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");
这也假设Po
的{{1}}集合称为Po_Part
。
您很可能必须完全打包partList
类。
顺便说一句,我建议您不要使用复数命名您的JPA实体类。换句话说,您的实体类应该被称为Shop_Orders
(或可能是Shop_Order
),因为此类的每个实例仅代表一个商店订单。