I have a table called CUSTOMER which contains 100+ COLOUMNS. But I want to select only 6 columns which I specified in my POJO:
Entity POJO:
@Entity @Data @Table(name = "CUSTOMER") public class CustomerEntity { @Id @Column(name = "C_ID") private String customerId; @Id @Column(name = "C_KEY") private String customerKey; @Column(name = "NAME") private String name; @Column(name = "FIRST_NAME") private String firstName; @Column(name = "LAST_NAME") private String lastName; @Column(name = "AGE") private String age; } NativeQuery:
String query = "select * from CUSTOMER where (C_ID= '1' AND C_KEY= '12') OR (C_ID= '1' AND C_KEY= '13')) AND AGE>25"; Query q = e.createNativeQuery(query,CustomerEntity.class); [Edit : For the reason to go with Native query]
Reason to choose Native Query :
- I need to execute the below complex query in JPA.
- In the complex query, I have subqueries, analytical method call. I assumed If I need to achieve this, then a native query will help.
- The above native query I wrote is just to test the inner most query.
Complex Logic:
SELECT * FROM ( SELECT row_number() over(order by C_ID, C_KEY) RN, FEW-COLUMNS( SELECT * FROM BOOK WHERE (C_ID, C_KEY) IN (customerId1, customerKey1) (customerId2, customerKey2) (customerId3, customerKey3) ..... (customerIdn, customerKeyn) AND ROWNUM <= 340 )WHERE RN BETWEEN anyNumber and anyNumber )ORDER BY DESC RN; Issues:
Since it is a namedQuery I am not able to pass the query as
String query = "select cu from CustomerEntity cu where ((cu.customerId = '1' AND cu.customerKey = '12') or (cu.customerId = '1' AND cu.customerKey = '13') AND cu.age > 25)";
If I use this query I am getting ORA -00947 Table or view doesn't exist Exception.
- Is it possible to get only the specific columns?