- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个带有嵌入式 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/
我有以下情况要解决,但无法正常工作(尝试了Hibernate和EclipseLink): Table_1: Column_A is Primary Key ... some other
我是 JPA 的新手,但必须在该技术中实现我的项目 我想做的是通过 CriteriaQuery 构建一些数据库查询,但不知道如何将参数列表传递给下面的代码: CriteriaBuilder qb =
我是 JPA 新手,注意到可以通过使用 @Version 注释实体中的字段来使用乐观锁定。我只是好奇,如果之前不存在,持久性提供程序是否会创建一个隐式版本字段。例如网站objectdb状态: "Whe
我有一个 JPA 查询 @Query(value = "SELECT SUM(total_price) FROM ... WHERE ...", nativeQuery = true) 当有匹配的记录
JPA 是否会尝试在已经持久(和非分离)的实体上级联持久化? 为了清楚起见,这是我的情况:我想保留一个新用户: public void addUser(){ //User is an enti
显然,OpenJPA。我也看到提到过 EclipseLink 和 Hibernate,但是在功能上有显着差异吗? 最佳答案 大多数差异来自提供者对 OSGi 的感知程度。例如,您可能需要自己将 Hib
我想将 JPA 用于 micronaut。为此,我使用 io.micronaut.data:micronaut-data-hibernate-jpa:1.0.0.M1 库。每当我运行应用程序并点击端点
我正准备为我的应用实现后端,现在我正在投影数据层。我期待着 Spring 。 最佳答案 Spring Data JPA 不是 JPA 实现。它提供了将数据访问层构建到底层 JPA 顶部的方法。您是否应
假设我有一个表 Item,其中包含一个名为 user_id 的列和一个表 User 以及另一个名为 Superuser 的列: CREATE TABLE Item(id int, user_id in
JPA 2.1 规范说: The entity class must not be final. No methods or persistent instance variables of the
我正在从事一个具有一些不寻常实体关系的项目,我在使用 JPA 时遇到了问题。有两个相关对象;用户,让我们称另一个 X。用户与 X 具有一对多和两个一对一的关系。它基本上看起来像这样 [用户实体] @O
我说的是 JavaEE 中的 JPA。在我读过的一本书中谈到: EntityManager em; em.find(Employee.class, id); “这是实体管理器在数据库中查找实例所需的所
我有 JPA 支持的 Vaadin 应用程序。此应用程序中的组件绑定(bind)到 bean 属性(通过独立的 EL 实现)。一些组件绑定(bind)到外部对象(或其字段),由@OneToOne、@O
是否可以使表中的外键唯一?假设我有实体 A 和 B。 答: @Entity class A extends Serializable { @Id private long id; @OneToOne
我在使用 JPA 时遇到了一点问题。考虑这种情况: 表 A (id_a) | 表 B (id_b, id_a) 我需要的是这样的查询: Select a.*, c.quantity from A as
我有一个由 JPA 管理的实体类,我有一个实体需要在其属性中记录更改。 JPA 是否提供任何方法来处理这种需求? 最佳答案 如果您使用 Hibernate 作为 JPA 提供程序,请查看 Hibern
我想实现以下架构: Table A: a_id (other columns) Table B: b_id (other columns) Table C: c_id (other columns)
我有一个愚蠢的问题。如果能做到的话那就太好了,但我并没有屏住呼吸。 我需要链接到我的 JPA 实体的表中的单个列作为所述 JPA 实体中的集合。有什么方法可以让我单独取回与该实体相关的列,而不必取回整
我有一个 Open JPA 实体,它成功连接了多对多关系。现在我成功地获取了整个表,但我实际上只想要该表中的 ID。我计划稍后调用数据库来重建我需要的实体(根据我的程序流程)。我只需要 ID(或该表中
这是我的一个实体的复合主键。 public class GroupMembershipPK implements Serializable{ private static final long
我是一名优秀的程序员,十分优秀!