Java 我在使用JPA访问字段时出错。我如何解决这个问题?

Java 我在使用JPA访问字段时出错。我如何解决这个问题?,java,mysql,hibernate,jpa,Java,Mysql,Hibernate,Jpa,在尝试将值插入到表中时,出现以下错误 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@6

在尝试将值插入到表中时,出现以下错误

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 Workbench,我正在尝试将我的值插入这里

我使用这个类在表中插入值

@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();
编辑:

我的客户表

我的用户表(我对该表进行了单元测试)


尝试使用自动或身份策略。 比如:


这是实体类中的ID字段定义

@Id
@GeneratedValue
@Column(name = "customer_id", unique = true, nullable = false)
private int id;
此处ID字段是唯一的,且不为null。因此,在插入过程中必须提供ID字段上的数据

首先,使用注释作为配置方法只是一种方便的方法,而不是处理无休止的XML配置文件

@Id
注释继承自
javax.persistence.Id
,表示下面的成员字段是当前实体的主键。因此,您的Hibernate和spring框架以及您可以基于此注释进行一些
reflect
工作。详情请查收

@GeneratedValue
注释用于配置指定列(字段)的增量方式

例如,当使用
Mysql
时,您可以在表的定义中指定
auto_increment
,使其自增量,然后使用

@GeneratedValue(strategy = GenerationType.IDENTITY)

你有
id
的getter吗?@ScaryWombat是的,我有。我在做一些单元测试,我的sql表中的id列似乎不能是
customer\u id
,它必须是
id
,才能正常工作。检查一下,如果没有正确的设置,列名注释可能会被忽略:@racraman这是一个奇怪的问题。当我进行单元测试时,这些值被插入到我的表中。由于我在控制器中执行所有这些插入操作,这就是错误的原因吗?@racraman我导入了这些属性,但没有任何效果。):我将
@GeneratedValue
替换为
@GeneratedValue(strategy=GenerationType.IDENTITY)
。我还将
@Column(name=“customer\u id”,unique=true,nullable=false)
替换为
@Column(name=“customer\u id”)
,它仍然给出相同的错误。你还有其他建议吗?
@GeneratedValue(strategy = GenerationType.IDENTITY)