在尝试向表中插入值时,我收到以下错误
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这是我的客户表。我正在使用MySql工作台,并且我正在尝试将我的值插入到这里。

并且我正在使用这个类将值插入到表中
@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这就是我如何将值插入到我的表中
// 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();编辑:
我的客户表

我的用户表(我在这个表上进行了单元测试)

发布于 2019-03-12 13:03:36
请尝试使用自动或身份策略。像这样:
@GeneratedValue(strategy = GenerationType.IDENTITY)发布于 2019-03-12 13:13:25
以下是实体类中的ID字段定义
@Id
@GeneratedValue
@Column(name = "customer_id", unique = true, nullable = false)
private int id;此处ID字段是唯一的,并且不为空。因此,您必须在插入过程中提供ID字段上的数据。
首先,使用注释作为我们的配置方法是一种方便的方法,而不是复制无穷无尽的XML配置文件。
@Id注释继承自javax.persistence.Id,表示下面的成员字段是当前实体的主键。因此,您的Hibernate和spring框架以及您可以基于此注释执行一些reflect工作。有关详细信息,请查看javadoc for Id
@GeneratedValue注释用于配置指定列(字段)的增量方式。
例如,在使用Mysql时,您可以在表的定义中指定auto_increment以使其自增量,然后使用
@GeneratedValue(strategy = GenerationType.IDENTITY)https://stackoverflow.com/questions/55113985
复制相似问题