亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 JPA 訪問字段時出現錯誤。我該如何解決這個問題?

使用 JPA 訪問字段時出現錯誤。我該如何解決這個問題?

墨色風雨 2022-07-27 21:19:01
嘗試將值插入表中時,出現此錯誤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@6d1c6e1ejavax.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 valuesCustomer 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 tableEntityManagerFactory emf = Persistence.createEntityManagerFactory("pu");EntityManager em = emf.createEntityManager();em.getTransaction().begin();em.persist(valuedCustomer);em.getTransaction().commit();
查看完整描述

2 回答

?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

嘗試使用 AUTO 或 IDENTITY 策略。喜歡:

@GeneratedValue(strategy = GenerationType.IDENTITY)


查看完整回答
反對 回復 2022-07-27
?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

這是您的實體類中的 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基于此注釋做一些工作。有關詳細信息,請檢查javadoc 的 Id


@GeneratedValue注解是配置指定列(字段)的遞增方式。


例如使用時Mysql,可以auto_increment在表的定義中指定使其自增,然后使用


@GeneratedValue(strategy = GenerationType.IDENTITY)


查看完整回答
反對 回復 2022-07-27
  • 2 回答
  • 0 關注
  • 237 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號