spring - JPA Hibernate 无法将 java.lang.String 字段设置为 java.lang.String 异常
问题描述
我正在为我的 webapp 使用以下技术:
- 春季启动 2.0
- 休眠 5.x
- 用于数据库的 postgresql
Web应用流程的简要概述: 1- 客户注册时,将客户实体保存在数据库(客户表)中 2- 只有在注册后,客户才能保存发货。因此,添加货件时,客户实体将始终存在于客户表中。3- 发货 -> 客户是多对一关系
尝试将货件实体保存在数据库中时出现以下异常:
org.springframework.orm.jpa.JpaSystemException: Error accessing field [private java.lang.String com.logistics.dao.model.Customer.email] by reflection for persistent property [com.logistics.dao.model.Customer#email] : abc@gmail.com; nested exception is org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.S`enter code here`tring com.logistics.dao.model.Customer.email] by reflection for persistent property [com.logistics.dao.model.Customer#email] : abc@gmail.com
有人可以帮我吗?我是否需要按照此处所述降级到 Hibernate 4.x
这里有一些代码供参考:
运输:
@Entity
@Table(name = "shipment")
public class ShipmentDB {
@Id
@Column(name = "shipment_id")
private String shipmentId;
@ManyToOne(targetEntity = Customer.class)
@JoinColumn(name = "email")
private String email;
@Column(name = "from_address_id")
private String fromAddressId;
@Column(name = "to_address_id")
private String toAddressId;
public String getShipmentId() {
return shipmentId;
}
public void setShipmentId(String shipmentId) {
this.shipmentId = shipmentId;
}
public String getFromAddressId() {
return fromAddressId;
}
public void setFromAddressId(String fromAddressId) {
this.fromAddressId = fromAddressId;
}
public String getToAddressId() {
return toAddressId;
}
public void setToAddressId(String toAddressId) {
this.toAddressId = toAddressId;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
顾客:
@Entity
@Table(name = "customer")
public class Customer {
@Id
@Column(name = "email")
private String email;
@Column(name = "password")
@Transient
private String password;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
装运仓库:
@Repository()
public interface ShipmentRepository extends JpaRepository<ShipmentDB, Long> {
ShipmentDB findByShipmentId(String shipmentId);
}
发货服务:
@Service("shipmentService")
public class ShipmentService {
@Autowired
private ShipmentRepository repository;
public ShipmentDB findShipmentById(String shipmentId) {
return repository.findByShipmentId(shipmentId);
}
public void saveShipment(ShipmentDB shipment) {
repository.save(shipment);
}
}
谢谢。
解决方案
关系应该在实体之间,但您正在应用字符串。
@ManyToOne(targetEntity = Customer.class)
@JoinColumn(name = "email")
private String email;
这是错误的。
将其更改为
@ManyToOne(targetEntity = Customer.class)
private Customer customer;
推荐阅读
- sql - 选择 IF 还是有更简单的方法?
- r - 为什么 Rcpp 中的 Portmanteau 测试比 R 中的慢?
- java - 在 Debain 上使用 blue cove 的 Java 蓝牙问题
- php - php面向对象范围访问数组
- bitcoin - 如何使用 bitcore 创建一个私有比特币网络?
- matlab - 通过模拟输出通道发送输出信号
- google-cloud-platform - 使用服务帐号导入 Google Cloud SQL 的权限
- javascript - 在现有 JSON 中添加另一个对象
- php - 如果 column1 匹配一个字符串,我需要什么 mysql 函数?
- string - 试图使用迭代器复制字符串的反向?