【问题标题】:I have an error when accessing a field with JPA. How do I solve this?使用 JPA 访问字段时出现错误。我该如何解决这个问题?
【发布时间】:2019-08-02 11:35:31
【问题描述】:

当尝试将值插入我的表时,我收到此错误

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 的吸气剂吗?
  • @ScaryWombat 是的,我愿意。我正在做一些单元测试,似乎我的 sql 表中的 id 列不能是 customer_id 它必须是 id 才能工作。
  • 检查一下,如果没有正确的设置,似乎列名注释可能会被忽略:stackoverflow.com/questions/25283198/…
  • @racraman 这是一个奇怪的问题。当我进行单元测试时,这些值会插入到我的表中。由于我正在将所有这些插入控制器中,这是错误的原因吗?
  • @racraman 我顺便导入了这些属性但无济于事。 ):

标签: java mysql hibernate jpa


【解决方案1】:

尝试使用 AUTO 或 IDENTITY 策略。 喜欢:

@GeneratedValue(strategy = GenerationType.IDENTITY)

【讨论】:

    【解决方案2】:

    这是您的实体类中的 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)
    

    【讨论】:

    • 我将@GeneratedValue 替换为@GeneratedValue(strategy=GenerationType.IDENTITY)。我还用@Column(name = "customer_id") 替换了@Column(name = "customer_id", unique = true, nullable = false),它仍然给出了同样的错误。您还有其他建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 2021-07-01
    • 1970-01-01
    • 2023-01-03
    • 1970-01-01
    • 2023-01-03
    • 2015-06-13
    相关资源
    最近更新 更多