gpt4 book ai didi

java - Spring无法识别实体参数

转载 作者:行者123 更新时间:2023-12-01 19:43:50 26 4
gpt4 key购买 nike

请问这个错误是什么原因

nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.Collection com.Vportal.data.reposito
ry.VisitRequestRepository.findBystaff_name(java.lang.String)! No property name found for type Staff! Traversed path: VisitRequest.staff.

在 Spring 运行网络应用程序后得到的?

staff.java

@Data
@Entity
public class Staff {

@Id
private int staff_id;
private String staff_name;
private String staff_email;

@OneToMany(targetEntity = VisitRequest.class, fetch = FetchType.LAZY, mappedBy = "staff")
private List<VisitRequest> visitRequest;


public String getstaff_name()
{
return staff_name;
}
public void setStaff_name(String staff_name)
{
this.staff_name = staff_name;
}
public String getstaff_email()
{
return staff_email;
}
public void setStaff_email(String staff_email)
{
this.staff_email = staff_email;
}

public void setstaff_id(int staff_id)
{
this.staff_id = staff_id;
}
public int getstaff_id()
{
return staff_id;
}


**

VisitRequest.java

@Entity
public class VisitRequest {
@Id
private String token;
private String comment;
private Date timestamp;
private boolean status;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "staff_name", referencedColumnName = "staff_name",nullable = false)
private Staff staff;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Visitor_id")
private Visitor visitor;



public String getToken(){
return token;
}
public void setToken(String token){
this.token = token;
}
public String getComment(){
return comment;
}
public void setComment(String comment){
this.comment = comment;
}
public Date getTimestamp(){
return timestamp;
}
public void setTimestamp(Date timestamp){
this.timestamp = timestamp;
}
}

VisitRequestRepository

@Repository
public interface VisitRequestRepository extends JpaRepository<VisitRequest, Long> {

Collection<VisitRequest> findBystaff_name (String staff_name);
}

我尝试更改实体的属性,更改存储库中的方法,但我只是不断收到相同的错误。我做错了什么吗?

最佳答案

正如评论中所说,您应该修复标签,例如从 staff_namestaffName。但如果您确实想坚持使用 staff_name 这个名字。你可以使用这个:

@Repository
public interface VisitRequestRepository extends JpaRepository<VisitRequest, Long> {

@Query("SELECT vr FROM VisitRequest vr WHERE vr.staff.staff_name = ?1")
Collection<VisitRequest> findIt (String staff_name);
}

或者

@Repository
public interface VisitRequestRepository extends JpaRepository<VisitRequest, Long> {

Collection<VisitRequest> findStaffStaff_name(String staff_name);
}

并更改 Staff 中 getter 和 setter 的命名,但如果您使用 lombok.Data 那么您不需要此代码(lombok 将生成它)。

关于java - Spring无法识别实体参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59148179/

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