gpt4 book ai didi

java - JpaRepository 找不到带有 EmbeddedID 的对象

转载 作者:行者123 更新时间:2023-12-01 09:38:40 25 4
gpt4 key购买 nike

我正在尝试使用 JpaRepository 通过 ID 从数据库中获取对象,但遇到了一些麻烦。它是一个具有 EmbeddedId 的实体。

我正在尝试以两种不同的方式获取对象:

  1. 使用命名查询 (findById)
  2. 使用SowServiceImpl中的方法

尝试使用命名方法获取它时遇到的异常是:

@Override
public SowDocument getById(int i) {
SowDocument wow = sowRepository.findById(i);
if (wow == null) {
System.out.println("NULLLLLLLLLLLLLLLL");
return null;
} else {
return wow;
}
}

HTTP Status 500 - Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Parameter with that position [1] did not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that position [1] did not exist
...
...
at com.sun.proxy.$Proxy791.findById(Unknown Source)
at com.**.pricing.web.services.impl.SowServiceImpl.getById(SowServiceImpl.java:61)
at com.**.pricing.web.controllers.UserController.getSowById(UserController.java:99)

当我尝试使用 EmbeddedId 获取它时,我得到一个 NullPointerException:

@Override
public SowDocument getById(int i) {
SowDocumentPK peek = new SowDocumentPK();
peek.setId(i);

SowDocument wow = sowRepository.findOne(peek);
if (wow == null) {
System.out.println("NULLLLLLLLLLLLLLLL");
return null;
} else {
return wow;
}
}

这是对象:

@Entity
@Table(name = "SowDocument", uniqueConstraints = {
@UniqueConstraint(columnNames = {"id"})})
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "SowDocument.findAll", query = "SELECT s FROM SowDocument s"),
@NamedQuery(name = "SowDocument.findById", query = "SELECT s FROM SowDocument s WHERE s.sowDocumentPK.id = :id"),
@NamedQuery(name = "SowDocument.findByClientName", query = "SELECT s FROM SowDocument s WHERE s.clientName = :clientName"),
@NamedQuery(name = "SowDocument.findByCreationDate", query = "SELECT s FROM SowDocument s WHERE s.creationDate = :creationDate"),
@NamedQuery(name = "SowDocument.findByDocumentCreator", query = "SELECT s FROM SowDocument s WHERE s.sowDocumentPK.documentCreator = :documentCreator"),
@NamedQuery(name = "SowDocument.findBySowType", query = "SELECT s FROM SowDocument s WHERE s.sowType = :sowType")})
public class SowDocument implements Serializable {

private static final long serialVersionUID = 1L;
@EmbeddedId
protected SowDocumentPK sowDocumentPK;
@Size(max = 50)
@Column(name = "clientName", length = 50)
private String clientName;
@Size(max = 45)
@Column(name = "creationDate", length = 45)
private String creationDate;
@Lob
@Column(name = "data")
private byte[] data;
@Size(max = 45)
@Column(name = "sowType", length = 45)
private String sowType;
@JoinColumn(name = "documentCreator", referencedColumnName = "id", nullable = false, insertable = false, updatable = false)
@ManyToOne(optional = false)
private User user;

public SowDocument() {
}

public SowDocument(SowDocumentPK sowDocumentPK) {
this.sowDocumentPK = sowDocumentPK;
}

public SowDocument(int id, int documentCreator) {
this.sowDocumentPK = new SowDocumentPK(id, documentCreator);
}

public SowDocumentPK getSowDocumentPK() {
return sowDocumentPK;
}

public void setSowDocumentPK(SowDocumentPK sowDocumentPK) {
this.sowDocumentPK = sowDocumentPK;
}

public String getClientName() {
return clientName;
}

public void setClientName(String clientName) {
this.clientName = clientName;
}

public String getCreationDate() {
return creationDate;
}

public void setCreationDate(String creationDate) {
this.creationDate = creationDate;
}

public byte[] getData() {
return data;
}

public void setData(byte[] data) {
this.data = data;
}

public String getSowType() {
return sowType;
}

public void setSowType(String sowType) {
this.sowType = sowType;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

@Override
public int hashCode() {
int hash = 0;
hash += (sowDocumentPK != null ? sowDocumentPK.hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof SowDocument)) {
return false;
}
SowDocument other = (SowDocument) object;
if ((this.sowDocumentPK == null && other.sowDocumentPK != null) || (this.sowDocumentPK != null && !this.sowDocumentPK.equals(other.sowDocumentPK))) {
return false;
}
return true;
}


}

这是其嵌入的 ID:

@Embeddable
public class SowDocumentPK implements Serializable {

@Basic(optional = false)
@Column(name = "id", nullable = false)
private int id;
@Basic(optional = false)
@NotNull
@Column(name = "documentCreator", nullable = false)
private int documentCreator;

public SowDocumentPK() {
}

public SowDocumentPK(int id, int documentCreator) {
this.id = id;
this.documentCreator = documentCreator;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getDocumentCreator() {
return documentCreator;
}

public void setDocumentCreator(int documentCreator) {
this.documentCreator = documentCreator;
}

@Override
public int hashCode() {
int hash = 0;
hash += (int) id;
hash += (int) documentCreator;
return hash;
}

@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof SowDocumentPK)) {
return false;
}
SowDocumentPK other = (SowDocumentPK) object;
if (this.id != other.id) {
return false;
}
return this.documentCreator == other.documentCreator;
}


}

这是 SowRepository 的代码:

public interface SowRepository extends JpaRepository<SowDocument, Serializable> {

SowDocument findById(int i);

}

这是 SowService 的代码:

@Service
public class SowServiceImpl implements SowService {

@Autowired
private SowRepository sowRepository;

@Override
public void save(SowDocument sow) {
sowRepository.save(sow);
}

@Override
public Collection<SowDocument> getAll() {
return sowRepository.findAll();
}

@Override
public SowDocument getById(int i) {
SowDocumentPK peek = new SowDocumentPK();
peek.setId(i);
return sowRepository.findOne(i);
}


}

我的猜测是,我在 SowDocument/SowDocumentPK 之间的映射有误,或者误解了如何使用 JpaRepository。

最佳答案

我猜您正在使用 SpringData JPA。如果这是正确的,请查看作为 JpaRepostory 接口(interface)的父类(super class)型 CrudRepository 是如何定义的:

public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
...
T findOne(ID id);
...
}

并且您正在扩展此接口(interface)(顺便说一句,您也应该发布实现):

public interface SowRepository extends JpaRepository<SowDocument, Serializable> {
SowDocument findById(int i);
}

findById(int i) 方法在 JpaRepository 类型层次结构中的none接口(interface)中定义,这意味着它是您自己的扩展。

另一方面,您没有 ID 类型为 int 的实体。您的实体 ID 的类型定义为由两个字段组成的 ShowDocumentPK 类型。

因此您的存储库定义应如下所示:

public interface SowRepository extends JpaRepository<SowDocument, ShowDocumentPK> {
SowDocument findById(ShowDocumentPK id);
}

并自行实现findById()方法(或者使用JpaRepository的官方实现类,即SimpleJpaRepository)。

然后您应该创建一个 ShowDocumentPK 实例并将其传递给 findById() 方法,例如:

SowDocumentPK peek = new SowDocumentPK();
peek.setId(1);
peek.setDocumentCreator(100);

SowDocument wow = sowRepository.findById(peek);

结论:您的 ID 不是 int 类型,并且 findById(int) 不是您案例中的实现方式。

希望这能让您了解如何正确实现它。

关于java - JpaRepository 找不到带有 EmbeddedID 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38620775/

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