jpa 2.0 - How to get the first member of the related collection in JPQL -
I have a related table images with a product table with which: 1. M
class product {personal integer product ID; Private string product name; .... .... .... Private listing & lt; Image & gt; ProductImageList; .... .... ....} Class Image {Private Integer Image ID; Private string image name; } Class Productlight {Private Integrity Product ID; Private string product name; Private string image name; } I am trying to query a JPQL query where I bring the products and productImageList first image and return to a ProductLite object using the new Creator.
@ Transaction Attribute (Transaction Auth Type .NOT_SUPPORTED) Public List & lt; ProductLite & gt; GetAllProductLite () {query q = em.createQuery (by selecting "new com.mycomp.application.entity.ProductLite (p.productId, p.productName, p.productImageList.get (0) .getImageName ())" + "from the product P "+" p.productName by order "); & Lt; ProductLite & gt; Prods = q.getResultList (); Return products; } But for some reason I am not able to work on it. I get a no vace accession. That's why I tried to move the product unit logic to get the first image (getImage () method so that I can call getImage () in the query. Even does not seem to work
Any help is appreciated.
First of all, you can not call methods in the unit class from your JPQ query. Second, the order of institutions in the list To use, you need a constant order
To create a column for the order on the joining table between the image and the product, you @OrderColumn ProductImageList to add -annotation. For example: do not define @OrderColumn (name = "myimage_order") // and name it by productImageList_order @OneToMany private list & lt; Image & gt; ProductImageList; Then you only need to modify the query to use the order to select the first image:
SELECT NEW com.mycomp. Products from P.ProductLite (p .productId, p.productName, pil.imageName) Product Imagingist Paul WHERE INDEX (pil) = 0 Order P. ProductNET
Comments
Post a Comment