1

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.

Mike3355
  • 11,305
  • 24
  • 96
  • 184
  • Shop_Orders should conform to the JavaBean contract so that you can make it an Entity. What is the downside? As an entity, you can obtain an EntityManager from a PersistenceContext (if the bean is application managed) and load your bean as you intend. If the bean is container managed then you get the EntityManager by dependency injection. – scottb May 25 '15 at 05:13
  • So are you saying I need to add another table to my database? I just changed the Shop_Orders class so it does conform to the JavaBean contract. However I did not add any annotations. I am still struggling tying this whole thing together. – Mike3355 May 25 '15 at 06:17
  • I just created a table called SHOP_ORDERS, add the "@"ENTITY annotation, "@"Columns etc and tried to run the query again and still get the error saying: cannot be cast to com.htd.repository.Shop_Orders – Mike3355 May 25 '15 at 07:28
  • Entities often correspond directly to database tables, but not necessarily. An entity may also represent a denormalized representation of a data model that corresponds to several tables. – scottb May 25 '15 at 10:12
  • @scottb Updated the comment, in hope I am more clear on what my issue is. – Mike3355 May 25 '15 at 10:49
  • so you sent off a query that returns an Object array and you seem to want to cast it to an Entity per row, and you can't do that. Either fix your query to return an Entity alias, or dont cast the results. No idea why the title is "How to initialise a model in JPA" when you have a problem with one query and its result – Neil Stockton May 25 '15 at 12:49
  • @NeilStockton is there a link you that you know of which shows an example? – Mike3355 May 25 '15 at 14:12
  • an example of what? You put some fields in the select clause so the return type is Object[]. If you just had some entity in the select ("SELECT p From Person p ...") then an Entity is the return type (for each row). – Neil Stockton May 25 '15 at 14:17
  • @NeilStockton If the object were to be initialized by the query then I would not have an issue. I have a SELECT query but how can I have that initialize my setters in the model? – Mike3355 May 25 '15 at 17:07

1 Answers1

1

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.

Community
  • 1
  • 1
DuncanKinnear
  • 4,563
  • 2
  • 34
  • 65