JPA Hibernate无法将java.lang.String字段设置为java.lang.String异常

时间:2018-08-29 04:54:10

标签: spring hibernate spring-boot jpa spring-data-jpa

我正在为我的Web应用程序使用以下技术:

  • Spring boot 2.0
  • 休眠5.x
  • 数据库的PostgreSQL

网络应用流程的简要概述: 1-当客户注册时,它将客户实体保存在数据库中(客户表) 2-只有注册后,客户才能保存发货。因此,添加货运时,客户实体将始终出现在客户表中。 3-发货->客户是ManyToOne关系

尝试将货运实体保存在数据库中时,出现以下异常:

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

有人可以帮我吗?我是否需要按照here

降级到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;
}

}

ShipmentRepository:

@Repository()
public interface ShipmentRepository extends JpaRepository<ShipmentDB, Long> {
ShipmentDB findByShipmentId(String shipmentId);
}

ShipmentService:

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

}

谢谢。

1 个答案:

答案 0 :(得分:0)

关系应该在实体之间,但是您要在String上应用。

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

这是错误的。

将其更改为

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