1

When trying to insert values into my table, i get this error

org.hibernate.property.access.spi.PropertyAccessException: Error accessing 
field [private int com.app.demo.model.Customer.id] by reflection for 
persistent property [com.app.demo.model.Customer#id] : 
com.app.demo.model.Customer@6d1c6e1e
javax.persistence.PersistenceException: 
org.hibernate.property.access.spi.PropertyAccessException: Error accessing 
field [private int com.app.demo.model.Customer.id] by reflection for 
persistent property [com.app.demo.model.Customer#id] : com.app.demo.model.Customer@6d1c6e1e

This is my customer table. I am using MySql Workbench, and I am trying to insert my values into here.

Table

And i am using using this class to insert values into the table

@Entity
@Table(name="customer")
public class Customer {

@Id
@GeneratedValue
@Column(name = "customer_id", unique = true, nullable = false)
private int id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="street_address")
private String address;
@Column(name="city")
private String city;
@Column(name="state")
private String state;
@Column(name="zip_code")
private String zipcode;
@Column(name="email")
private String email;
@Column(name="paypal_email")
private String paypalEmail;


// getters and setters

And this is how I am inserting values into my table

// Set customer values
Customer valuedCustomer = new Customer();

valuedCustomer.setFirstName(firstName);
valuedCustomer.setLastName(lastName);
valuedCustomer.setAddress(address);
valuedCustomer.setCity(city);
valuedCustomer.setState(state);
valuedCustomer.setZipcode(zip);
valuedCustomer.setEmail(email);

// insert customer info into the customer table
EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu");

EntityManager em = emf.createEntityManager();

em.getTransaction().begin();
em.persist(valuedCustomer);
em.getTransaction().commit();

EDIT:

My customer table

customer table

My user table (this table i did the unit test on)

user table

Tony
  • 83
  • 1
  • 9
  • do you have a getter for `id` ? – Scary Wombat Mar 12 '19 at 04:16
  • @ScaryWombat Yes i do. I was doing some unit testing, and it seems like my column for id in my sql table cant be `customer_id` it has to be `id` in order for it to work. – Tony Mar 12 '19 at 04:24
  • Check this out, seems like column name annotations could be being ignored without the right settings : https://stackoverflow.com/questions/25283198/spring-boot-jpa-column-name-annotation-ignored – racraman Mar 12 '19 at 04:37
  • @racraman this is a weird problem. When I do unit tests, the values get inserted in my table. Since i am doing all of this inserting in a controller, is this the reason for the error? – Tony Mar 12 '19 at 04:44
  • @racraman I imported those properties btw with no avail. ): – Tony Mar 12 '19 at 04:53
  • Can you do "show columns" for both your actual customer table, and also the other temporary table that works (where you've renamed the column from "customer_id" to "id"). I'm guessing that you've declared the latter "not null" ? :) – racraman Mar 12 '19 at 05:31
  • hey @racraman, i added pictures to the post. I am not sure why this isn't working on either one when i use this inside my controller. The unit test works fine. ): – Tony Mar 12 '19 at 06:27
  • Thanks @Tony - drats, so it's not "not null". PropertyAccessException used to be thrown when a nullable column was mapped to a primitive, so thought that might still be the case, and be the case here - but you've got them not null, so should be good. – racraman Mar 12 '19 at 06:44

1 Answers1

2

try using AUTO or IDENTITY policy. like:

@GeneratedValue(strategy = GenerationType.IDENTITY)
PatrickChen
  • 1,350
  • 1
  • 11
  • 19