首页 > 解决方案 > JPA Hibernate 无法将 java.lang.String 字段设置为 java.lang.String 异常

问题描述

我正在为我的 webapp 使用以下技术:

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);
}

}

谢谢。

标签: springhibernatespring-bootjpaspring-data-jpa

解决方案


关系应该在实体之间,但您正在应用字符串。

@ManyToOne(targetEntity = Customer.class)
@JoinColumn(name = "email")
private String email;

这是错误的。

将其更改为

@ManyToOne(targetEntity = Customer.class)
private Customer customer;

推荐阅读