javaspring-bootjpaspring-data-jpajparepository

Can I pass Java Object in JpaRepository Class?


import org.springframework.stereotype.Repository;

@Repository
public interface CustomRepository extends JpaRepository<Object, Long> {

     @Query(value = "SELECT name FROM student", nativeQuery = true)
    List<String> getAllStudentNames();
}

Will this work if not what will be the solution for this? I don't want to create student Entity.


Solution

  • If you do not have a Student class then you should not make a JpaRepository for it. This is my suggestion:

    @Repository
    public class StudentRepository {
        @PersistenceContext
        private EntityManager em;
    
        public List<String> findAllStudentNames() {
            return em.createNativeQuery("SELECT name FROM student").getResultList();
        }
    }