I have a class with an embedded Id. When I try to search using Jpa Repository, it returns nothing but a null Object. It Looks like the problem is in the Embedded Id, sice I made a test with a class without this and it worked fine.
The query outputed by JPA in the console works fine when I test it agaisnt the database.
And no errors are outputed.
EDIT: There is data in the database
EDIT2: Added equals and hashcode.
EDIT3: The findAll method works.
Entity
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.joda.time.DateTime;
@Entity
@Table(name="C_INFO_CADASTRO")
public class Cliente implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private ClienteId id;
@Column(name="DAT_NASC")
private DateTime dataNascimento;
@Column(name="TXT_EMAIL")
private String email;
@Column(name="NOM_CLIENTE")
private String nomeCliente;
@Column(name="NUM_CPF")
private Long numeroCpf;
public ClienteId getId() {
return id;
}
public void setId(ClienteId id) {
this.id = id;
}
public DateTime getDataNascimento() {
return dataNascimento;
}
public void setDataNascimento(DateTime dataNascimento) {
this.dataNascimento = dataNascimento;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNomeCliente() {
return nomeCliente;
}
public void setNomeCliente(String nomeCliente) {
this.nomeCliente = nomeCliente;
}
public Long getNumeroCpf() {
return numeroCpf;
}
public void setNumeroCpf(Long numeroCpf) {
this.numeroCpf = numeroCpf;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((numeroCpf == null) ? 0 : numeroCpf.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Cliente other = (Cliente) obj;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (numeroCpf == null) {
if (other.numeroCpf != null)
return false;
} else if (!numeroCpf.equals(other.numeroCpf))
return false;
return true;
}
}
EmbeddedId
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class ClienteId implements Serializable{
private static final long serialVersionUID = 1L;
@Column(name="COD_EMP")
private Long codigoEmpresa;
@Column(name="COD_FIL")
private Long codigoFilial;
@Column(name="NUM_CLI")
private Long numeroCliente;
public Long getCodigoEmpresa() {
return codigoEmpresa;
}
public void setCodigoEmpresa(Long codigoEmpresa) {
this.codigoEmpresa = codigoEmpresa;
}
public Long getCodigoFilial() {
return codigoFilial;
}
public void setCodigoFilial(Long codigoFilial) {
this.codigoFilial = codigoFilial;
}
public Long getNumeroCliente() {
return numeroCliente;
}
public void setNumeroCliente(Long numeroCliente) {
this.numeroCliente = numeroCliente;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((codigoEmpresa == null) ? 0 : codigoEmpresa.hashCode());
result = prime * result + ((codigoFilial == null) ? 0 : codigoFilial.hashCode());
result = prime * result + ((numeroCliente == null) ? 0 : numeroCliente.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ClienteId other = (ClienteId) obj;
if (codigoEmpresa == null) {
if (other.codigoEmpresa != null)
return false;
} else if (!codigoEmpresa.equals(other.codigoEmpresa))
return false;
if (codigoFilial == null) {
if (other.codigoFilial != null)
return false;
} else if (!codigoFilial.equals(other.codigoFilial))
return false;
if (numeroCliente == null) {
if (other.numeroCliente != null)
return false;
} else if (!numeroCliente.equals(other.numeroCliente))
return false;
return true;
}
}
Repository
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;
import io.swagger.annotations.Api;
@Api
@Component
public interface ClienteRepository extends JpaRepository {
Cliente findByNumeroCpf(@Param("numeroCpf") Long numeroCpf);
Cliente findByEmail(@Param("email") String email);
}
Service
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Component
@Path("/cliente")
@Transactional
public class ClienteService {
@Inject
private ClienteRepository repository;
@Inject
private CodMarcaRepository marcaRepository;
@GET
@Path("/cpf/{numeroCpf}")
@Produces(MediaType.APPLICATION_JSON)
@Transactional(readOnly=true)
public Response findByCpf(@PathParam("numeroCpf") Long numeroCpf){
Cliente cliente = repository.findByNumeroCpf(numeroCpf);
if(cliente != null){
return Response.ok().entity(cliente).build();
} else {
return Response.status(404).entity(new Cliente()).build();
}
}
@GET
@Path("/email/{email}")
@Produces(MediaType.APPLICATION_JSON)
public Response findByEmail(@PathParam("email") String email){
Cliente cliente = repository.findByEmail(email);
if(cliente != null){
return Response.ok().entity(cliente).build();
} else {
return Response.status(404).entity(new Cliente()).build();
}
}
}
As you can see I have two methods in my service, one to find by cpf and one to find by email both did not work. I also tried to create a method to find by the composed primary key and it also did not work.
Any help will be apreciated, since I don't know what else to do.