0

I'm newbie with spring and I'm trying create an apiRest

@Repository
public interface IPersonajeDao extends JpaRepository<Personaje, Long> {

    @Query( nativeQuery = true, value="SELECT imagen,nombre FROM personaje")
    public List<Personaje> getAll();

I have this query in the repository, this query should get the fields image and name from the table "personaje"

When I running the aplication I get this error = "java.sql.SQLException: Column 'id' not found." Why it is?

service:

    @Override
    @Transactional
    public List<Personaje> getAll() {
        return this.personajeRepository.getAll();
    }

Entity:

    @Id
    private long id;
    private String imagen;
    private String nombre;
    private int edad;
    private String historia;
    
    

     public Personaje(long id, String imagen, String nombre, int edad, String historia) {
        this.imagen = imagen;
        this.nombre = nombre;
        this.edad = edad;
        this.historia = historia;
    }
     
     public Personaje() {
         
     }


    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
     @JoinTable(
       name = "participa",
       joinColumns = @JoinColumn(name = "FK_PERSONAJE", nullable = false),
       inverseJoinColumns = @JoinColumn(name="FK_SERIE", nullable = false))
     private List<Serie> series;

}
loverotaku
  • 13
  • 3
  • Why are you using a native query for get all? Especially when the entity more fields than your query? I assume that you need to add the Id to your query as you are returning a list of entities which need the id as they are existing entities. Otherwise hibernate can not verify that these are existing entities AFAIR – Daniel Rafael Wosch May 29 '21 at 19:21
  • How is the database schema ceated? Through JPA or explicitly? If the schema is created explicitly, please show the schema. – Turing85 May 29 '21 at 19:26
  • because, i want get all but only with the fields "imagen" and "nombre" – loverotaku May 29 '21 at 19:27
  • Take a look to https://stackoverflow.com/questions/2355728/jpql-create-new-object-in-select-statement-avoid-or-embrace#2359214 – kache May 29 '21 at 19:50

1 Answers1

0

You have missed this.id = id

    public Personaje(long id, String imagen, String nombre, int edad, String historia) {
    this.imagen = imagen;
    this.nombre = nombre;
    this.edad = edad;
    this.historia = historia;
       }
Demnofocus
  • 21
  • 6