1

I am new to spring boot and StackOverflow as well. So kindly ignore any mistakes.

Actually, I want to access and print the data in a table using Spring JPA or Spring Data, let say the table has 100 columns, and I want to print only 2 columns let's say id and name. Do I have to create all the 100 fields in my model? Is there any way that I could connect to it by making a model with id and name variables only?

For example, there is a product table, let's say on the redshift database on aws, the table name is Product and it has 200 columns. I am creating a rest API where I need just 2 things, id and name to be shown. something like:

@Entity("Product") 
class Product {
  @Id private Long id; 
  private String name;
}

but when I do this I get the exception of: Schema not found

Is it necessary to specially all the 200 fields in this class Product?

Asim
  • 11
  • 3
  • What have you tried? Did you do any prior research? Does this https://stackoverflow.com/questions/22007341/spring-jpa-selecting-specific-columns answer your question? – GhostCat Feb 04 '21 at 08:15
  • for example lets say in data base we have tables named student having fields like id, name, age , gender, address.... like 100s of field, and i am creating a rest api where i need the table but just need id, and name, so i want to create a model and repo for that. so is there any method through which i could make model Student with id and name and its repo accordingly? instead of wasting time writing the other fields – Asim Feb 04 '21 at 08:18
  • yes i have go through the link you sent, but it applies to the scenario where we build a model "products" with all the fields lets say 100 and then we write the native query to get desired columns, i don't want that. My question is, is there any way through which we don't need to have specify all the columns in the model it self. e.g. @Entity("Product") class product {@Id private Long id; private String name} just this, instead of specifying all the 100 colums in the model – Asim Feb 04 '21 at 08:32
  • 2
    Experimenting yourself and indeed following the given link is expected and probably will give you the answer you are looking for. Just create a model with only the needed columsn and see if you get any exceptions. Then update your question with the exception and some code samples – Milo van der Zee Feb 04 '21 at 08:35
  • @MilovanderZee van der Zee do you know the answer? – Asim Feb 04 '21 at 09:09
  • I'm sure you don't need to define all tables. The message "Schema not found" probably means that the table is not found in your database. – Milo van der Zee Feb 05 '21 at 09:17

0 Answers0