2

I'm using the following technologies for my webapp:

  • Spring boot 2.0
  • Hibernate 5.x
  • postgresql for database

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.

4
  • do you want to create a customer in your database when persisting Shipment ? Commented Aug 29, 2018 at 5:03
  • Here is the flow: 1- When customer registers, it saves customer entity in the database (customer table) 2- Only after sign up, customer can save shipment. So, customer entity will always be present in customer table when shipment gets added. Commented Aug 29, 2018 at 5:04
  • transient fields are not meant to be persisted. This '@Column' and '@Transient' annotations seem very strange to me. Commented Aug 29, 2018 at 9:38
  • @garfield - you're right. that was a copy paste typo. thanks for pointing out Commented Aug 29, 2018 at 21:12

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

1 Comment

thanks this works. So @ManyToOne picks up the PK of target entity and sets it as a FK constraint on ShimentDB table?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.