gpt4 book ai didi

java - Spring-Data-JPA - 查询返回 null - EmbeddedId

转载 作者:太空宇宙 更新时间:2023-11-04 11:08:04 24 4
gpt4 key购买 nike

我有一个带有嵌入式 ID 的类。当我尝试使用 Jpa 存储库进行搜索时,它只返回一个 null 对象。看起来问题出在嵌入式 Id 中,因为我用一个没有这个的类进行了测试,并且工作正常。

当我针对数据库进行测试时,JPA 在控制台中输出的查询工作正常。

并且没有输出任何错误。

编辑:数据库中有数据

EDIT2:添加了等于和哈希码。

EDIT3:findAll 方法有效。

实体

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;
}
}

嵌入式Id

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;
}
}

存储库

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, ClienteId> {

Cliente findByNumeroCpf(@Param("numeroCpf") Long numeroCpf);

Cliente findByEmail(@Param("email") String email);
}

服务

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();
}
}
}

正如你所看到的,我的服务中有两种方法,一种通过 cpf 查找,一种通过电子邮件查找,但这两种方法都不起作用。我还尝试创建一种通过组合主键查找的方法,但它也不起作用。

任何帮助将不胜感激,因为我不知道还能做什么。

最佳答案

我不确定这是否是您遇到的问题的根源,但您没有重写 Embeddable 类的 equals()/hashCode() 。如果您想将该类用作 @EmbeddedId,这是强制性的。

关于java - Spring-Data-JPA - 查询返回 null - EmbeddedId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46285992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com