Spring JPA 参数值 [1000] 与预期类型不匹配 [ma.mycom.myapp.domain.Client (n/a)]
Spring JPA Parameter value [1000] did not match expected type [ma.mycom.myapp.domain.Client (n/a)]
我有两个名为 Appointement
和 Client
的实体,我想获得所有
指定客户的任命。当我尝试这样做时,我遇到了以下错误消息。
2022-05-24 13:30:41.685 WARN 23252 --- [ XNIO-3 task-1]
.m.m.a.ExceptionHandlerExceptionResolver : Resolved
[org.springframework.dao.InvalidDataAccessApiUsageException: Parameter
value [1000] did not match expected type [ma.mycom.myapp.domain.Client
(n/a)]; nested exception is java.lang.IllegalArgumentException:
Parameter value [1000] did not match expected type
[ma.mycom.myapp.domain.Client (n/a)]]
这是我的实体:
Client.java
@Entity
@Table(name = "CLIENTS")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
@Column(name = "id")
private Long id;
//other attributes, getters, setters
}
Appointment.java
@Entity
@Table(name = "APPOINTMRNTS")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Appointment implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
@Column(name = "id")
private Long id;
//other attributes
@ManyToOne
@JoinColumn(name = "ID_CLIENT")
private Client client;
//getters, setters
}
控制器,以及我如何调用查询:
List<Appointement> clientAppointements = appointementRepository.findAllByClient(idClient);
这是AppointementRepository.java
中使用的查询(我猜这是问题的根源)
@Query(
name = "SELECT pe.* FROM APPOINTEMENTS pe INNER JOIN CLIENTS e ON pe.ID_CLIENT = e.ID WHERE e.id = ?1",
nativeQuery = true
)
List<Appointement> findAllByClient(Long idClient);
您的 Appointment
class 没有任何类型的名为“客户端 ID”的字段,它只知道它拥有的 Client
实体。
在您的 JPA 存储库方法中,您只能使用实体的现有字段。
解决此问题的两种最标准方法是:
1- 将 Appointment
字段添加到关系的 Client
端以使其成为双向的(我个人推荐这样做)。您的 Client
实体将如下所示:
@Entity
@Table(name = "CLIENTS")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
@Column(name = "id")
private Long id;
@OneToMany(mappedBy = "client", fetch = FetchType.LAZY)
List<Appointment> appointments;
//other attributes, getters, setters
}
然后您可以通过获取 Client
对象,然后访问其 appointments
字段来简单地获取约会。
List<Appointement> clientAppointements = clientRepository.findClientByField(field)
.getAppointments();
或者您甚至可以在存储库方法中获得客户的约会:
// in your appointment repository
@Query(value = "SELECT c.appointments FROM Client c WHERE c.id = :cId")
List<Appointment> getAppointmentsById(Long cId);
2- 如果您不想建立双向关系,您应该在使用 Appointment
存储库方法搜索之前获取 Client
对象。
// in your appointment repository
List<Appointment> findByClient(Client client);
// then you can fetch it anywhere
Client client = clientRepository.findById(cliendId);
List<Appointment> clientAppointments = appointmentRepository.findByClient(client);
我有两个名为 Appointement
和 Client
的实体,我想获得所有
指定客户的任命。当我尝试这样做时,我遇到了以下错误消息。
2022-05-24 13:30:41.685 WARN 23252 --- [ XNIO-3 task-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [1000] did not match expected type [ma.mycom.myapp.domain.Client (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [1000] did not match expected type [ma.mycom.myapp.domain.Client (n/a)]]
这是我的实体:
Client.java
@Entity
@Table(name = "CLIENTS")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
@Column(name = "id")
private Long id;
//other attributes, getters, setters
}
Appointment.java
@Entity
@Table(name = "APPOINTMRNTS")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Appointment implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
@Column(name = "id")
private Long id;
//other attributes
@ManyToOne
@JoinColumn(name = "ID_CLIENT")
private Client client;
//getters, setters
}
控制器,以及我如何调用查询:
List<Appointement> clientAppointements = appointementRepository.findAllByClient(idClient);
这是AppointementRepository.java
中使用的查询(我猜这是问题的根源)
@Query(
name = "SELECT pe.* FROM APPOINTEMENTS pe INNER JOIN CLIENTS e ON pe.ID_CLIENT = e.ID WHERE e.id = ?1",
nativeQuery = true
)
List<Appointement> findAllByClient(Long idClient);
您的 Appointment
class 没有任何类型的名为“客户端 ID”的字段,它只知道它拥有的 Client
实体。
在您的 JPA 存储库方法中,您只能使用实体的现有字段。
解决此问题的两种最标准方法是:
1- 将 Appointment
字段添加到关系的 Client
端以使其成为双向的(我个人推荐这样做)。您的 Client
实体将如下所示:
@Entity
@Table(name = "CLIENTS")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
@Column(name = "id")
private Long id;
@OneToMany(mappedBy = "client", fetch = FetchType.LAZY)
List<Appointment> appointments;
//other attributes, getters, setters
}
然后您可以通过获取 Client
对象,然后访问其 appointments
字段来简单地获取约会。
List<Appointement> clientAppointements = clientRepository.findClientByField(field)
.getAppointments();
或者您甚至可以在存储库方法中获得客户的约会:
// in your appointment repository
@Query(value = "SELECT c.appointments FROM Client c WHERE c.id = :cId")
List<Appointment> getAppointmentsById(Long cId);
2- 如果您不想建立双向关系,您应该在使用 Appointment
存储库方法搜索之前获取 Client
对象。
// in your appointment repository
List<Appointment> findByClient(Client client);
// then you can fetch it anywhere
Client client = clientRepository.findById(cliendId);
List<Appointment> clientAppointments = appointmentRepository.findByClient(client);