【问题标题】:JPA Hibernate Can not set java.lang.String field to java.lang.String ExceptionJPA Hibernate 无法将 java.lang.String 字段设置为 java.lang.String 异常
【发布时间】:2018-08-29 04:54:09
【问题描述】:

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

  • Spring Boot 2.0
  • 休眠 5.x
  • 用于数据库的 postgresql

网络应用流程的简要概述: 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

有人可以帮我解决这个问题吗?我是否需要按照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);
}

发货服务:

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

}

谢谢。

【问题讨论】:

  • 在持久化 Shipment 时是否要在数据库中创建客户?
  • 流程如下: 1- 客户注册时,将客户实体保存在数据库(客户表)中 2- 客户注册后才能保存发货。因此,添加货件时,客户实体将始终存在于客户表中。
  • 瞬态字段并不意味着被持久化。这个 '@Column' 和 '@Transient' 注释对我来说似乎很奇怪。
  • @garfield - 你是对的。那是一个复制粘贴错字。谢谢指出

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


【解决方案1】:

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

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

这是错误的。

改成

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

【讨论】:

  • 感谢这项工作。那么@ManyToOne 会获取目标实体的 PK 并将其设置为 ShimentDB 表的 FK 约束?
猜你喜欢
  • 2016-01-23
  • 2016-12-18
  • 2018-07-18
  • 1970-01-01
  • 2021-12-14
  • 2022-10-30
  • 1970-01-01
  • 2017-02-18
相关资源
最近更新 更多