Mike3355
Mike3355

Reputation: 12061

How to initialize a model in JPA

I am simply trying to initialize an object inside a list via a JPA query. Been unsuccessful in my 1 1/2 day journey thus far.

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 Model

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

I created table for shop_orders:Shop_Order diagram from mySQL

Here is an example of what I would like to do once query is ran and the object is set in the list.

Shop_Orders shopOrders;
int poNumber = shopOrders.getPo_number(); 

However I keep getting the following error when I run the query

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

I will post snippets of Po.java and Part.java in case someone wants to look at them.

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

Thanks for the advice ahead of time.

Upvotes: 1

Views: 421

Answers (1)

DuncanKinnear
DuncanKinnear

Reputation: 4643

If you have a table which contains the data that corresponds to your Shop_orders Entity, then your query should just be:

@Query("SELECT so FROM Shop_Orders so")

Since you have mapped the Shop_Orders Entity to that table.

However, since you seem to be constructing a de-normalised model of the 3 tables involved (PO, Part and the join table between the two, PO_Part), then perhaps you need to create a de-normalised database VIEW for the original query you had, and then map that view to your Shop_Orders entity by specifying the view in your @Table annotation.

Alternatively, you could create a constructor for Shop_Orders (which doesn't need to be a JPA Entity) that takes all the columns. In other words:

public Shop_Orders(int po_id, int po_number, Date po_due_date, etc... )

and then, assuming that you have an Entity Po_Part for the join table between Po and Part (see my answer to your other question), the query can be:

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

That's also assuming that Po has a collection of Po_Part called partList.

You would most likely have to fully package-path the Shop_Orders class.

By the way, I would recommend that you do not name your JPA Entity classes using plurals. In other words, your Entity class should be called Shop_Order (or possibly ShopOrder) as each instance of this class would only represent one Shop Order.

Upvotes: 1

Related Questions