0

I have an entity with 500 columns. However, i only need 100 columns in a few scenarios.The required column list changes as per configuration.

e.g

Query query = entityManager.createNativeQuery("Select NAME, AGE from CUSTOMER",Customer.class); 

It is throwing an error for missing columns.

Is there any way to load selected columns in entity and ignore others.

Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24
Sumit Tyagi
  • 431
  • 2
  • 14
  • Please post your `Customer` class. And also go through [Spring JPA selecting specific columns](https://stackoverflow.com/questions/22007341/spring-jpa-selecting-specific-columns) – Sudhir Ojha Sep 09 '19 at 09:18
  • Why does your entity have 500 columns? There's about 450 columns too many in there. – Kayaman Sep 09 '19 at 09:20
  • As I stated in the question, the Column list is not fixed. So I can not write a fixed query or Resultset mapping. User can change the configuration in running application and column set change accordingly. For example, I may need to load the name and age both column or only name. So I need something dynamic so that I can select fields as per configuration and load entities – Sumit Tyagi Sep 09 '19 at 10:34

1 Answers1

0

You can use projections see Projections tutorial

Criteria criteria = session.createCriteria(Product.class);
criteria = criteria.setProjection(
Projections.projectionList()
.add(Projections.id())
.add(Projections.property("name")));