1

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 :

  1. I need to execute the below complex query in JPA.
  2. In the complex query, I have subqueries, analytical method call. I assumed If I need to achieve this, then a native query will help.
  3. 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:

  1. 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.

  1. Is it possible to get only the specific columns?
3
  • The second query you wrote doesn't look ok. What is 'ct' in this query? what is 'cu' ? is that the exact query you use? Commented May 24, 2017 at 10:20
  • String query = "select ct from CustomerEntity ct where ((ct.customerId = '1' AND ct.customerKey = '12') or (ct.customerId = '1' AND ct.customerKey = '13') AND ct.age > 25)"; maybe like that Commented May 24, 2017 at 10:20
  • @Rjiuk, I meant to keep alias name(cu) for the Entity POJO. Now I edited the question. Commented May 24, 2017 at 10:25

1 Answer 1

5

You have to use aliases in order for this to work. Otherwise the persistence provider will not know how to perform the match:

select c_id as customerId, c_key as customerKey... from CUSTOMER where ... 

Aliases should be exactly the same as corresponding field names of the entity class your mapping to.

Sign up to request clarification or add additional context in comments.

8 Comments

Do you mean I need to to use the select query with aliases for every column in the method createNativeQuery. (For the issue table or view does not exist)
yes, you plan to have only 6 columns in the select so its not much of hastle
Ok. But in of my another entity I need to select more than 40 columns. In that case it will be a big query If I select each column name? Any alternative way to simplfy th query
how do expect to specify a subset of columns without doing it explicitly? Or maybe you want to select all the columns? Why use native query in that case then?
Native query will in general always be faster. You simply loose though all the orm benefits, including the database change rewrite. Your query though because of the complexity.. i would create a stored procedure out of it and use JPA API to call procedure. IF the db changes you wold only need to rewrtie the procedure without changing the java code / configuration.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.