Create query in a repository in spring

3

I have a repository and in its interface I want to create a function that requests a String and returns an object of type user, in mysql the syntax would be like this:

SELECT C.NOMBRE FROM CLIENTE C, ALIAS A WHERE A.NOMBRE = 'bar' AND A.CLIENTE_ID = C.ID

However, how would it be in the interface?

this is my class:

package app.core.repository;

import app.core.entity.Cliente;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface ClienteRepository extends JpaRepository<Cliente, Integer>{

@Query("SELECT Cliente FROM CLIENTE, ALIAS A WHERE A.NOMBRE = 'bar' AND A.CLIENTE_ID = C.ID")
Cliente getClientePorAlias(@Param("alias") String alias);

}
    
asked by gibran alexis moreno zuñiga 02.03.2017 в 17:49
source

2 answers

1

Finally my function was like this:

@Query("SELECT A.cliente FROM Alias A WHERE lower(A.nombre) = lower(:alias)")
Cliente findClienteByAlias(@Param("alias") String alias);
    
answered by 16.03.2017 / 22:01
source
0

With Spring Data JPA it would be like this:

public interface ClienteRepository extends JpaRepository<Cliente, Integer>{

@Query("SELECT c FROM Cliente c WHERE c.nombre= :alias AND c.cliente_id= :id")
Cliente getClientePorAlias(@Param("alias") String alias,@Param("id")Integer id);

}

    
answered by 04.03.2017 в 08:34