I have a simple System ( Controller, Service, Repository ) but even if the data are present on the database the service returns a null value
Data
mysql> select * from customer; +----+---------------+ | id | username | +----+---------------+ | 4 | liparistudios | +----+---------------+
Domain
@Data @Entity @Table(name = "customer") public class Customer implements Serializable { private static final long serialVersionUID = 201811031445L; @Id @GeneratedValue( strategy = GenerationType.SEQUENCE ) private Long id; private String username;
Controller
Customer c = customerService.searchCustomerByUsername( usernameToFind );
Service
@Service public class CustomerService { @Autowired private CustomerRepo repo; public Customer searchCustomerByUsername( String username ) { return repo.findAllByUsername( username ); }
Repository
@Repository @Transactional public interface CustomerRepo extends JpaRepository<Customer, Long> { @Query( value = "SELECT * FROM Customer c WHERE username = ':username' ORDER BY username ASC LIMIT 1", nativeQuery = true ) public Customer findAllByUsername(@Param("username") String username );
Advertisement
Answer
Bind parameters should not be wrapped inside quotes. Make sure to remove single quotes around :username
.
Correct query would be SELECT * FROM Customer c WHERE username = :username ORDER BY username ASC LIMIT 1