tim_la_amzn
tim_la_amzn

Reputation: 35

JPA Hibernate Can not set java.lang.String field to java.lang.String Exception

I'm using the following technologies for my webapp:

Brief overview of the web app flow: 1- When customer registers, it saves customer entity in the database (customer table) 2- Only after the sign up, customer can save shipment. So, customer entity will always be present in customer table when shipment gets added. 3- Shipment -> Customer is a ManyToOne relationship

I'm getting the following exception when trying to save the shipment entity in database:

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] : [email protected]; 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] : [email protected]

Can someone please help me with this? Do I need to downgrade to Hibernate 4.x as described here

Here is some code for reference:

Shipment:

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

}

Customer:

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

}

Thanks.

Upvotes: 2

Views: 3905

Answers (1)

Alien
Alien

Reputation: 15878

The relationships should be in between entities but you are applying on String.

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

which is wrong.

change it to

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

Upvotes: 2

Related Questions