【问题标题】:Hibernate IllegalArgumentException while persisting entity object: Cannot set field 'id' to entity object?持久化实体对象时出现休眠 IllegalArgumentException:无法将字段“id”设置为实体对象?
【发布时间】:2019-12-28 12:26:22
【问题描述】:

我尝试持久化一个实体对象,但我得到了一个令人讨厌的 IllegalArgumentException。查看堆栈跟踪并没有给我太多信息。不知何故,hibernate 尝试将实体对象的 id 字段设置为实际实体。

这是堆栈跟踪:

Caused by: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.Long com.hosting.hostinginterface.spring.user.User.id] by reflection for persistent property [com.hosting.hostinginterface.spring.user.User#id] : com.hosting.hostinginterface.spring.user.User@246a5eb6
    at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:75) ~[hibernate-core-6.0.0.Alpha3.jar:6.0.0.Alpha3]
    at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:5411) ~[hibernate-core-6.0.0.Alpha3.jar:6.0.0.Alpha3]
    at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:5036) ~[hibernate-core-6.0.0.Alpha3.jar:6.0.0.Alpha3]
    at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:292) ~[hibernate-core-6.0.0.Alpha3.jar:6.0.0.Alpha3]
    at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:529) ~[hibernate-core-6.0.0.Alpha3.jar:6.0.0.Alpha3]
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:102) ~[hibernate-core-6.0.0.Alpha3.jar:6.0.0.Alpha3]
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:62) ~[hibernate-core-6.0.0.Alpha3.jar:6.0.0.Alpha3]
    at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:108) ~[hibernate-core-6.0.0.Alpha3.jar:6.0.0.Alpha3]
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:680) ~[hibernate-core-6.0.0.Alpha3.jar:6.0.0.Alpha3]
    ... 76 common frames omitted
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Long field com.hosting.hostinginterface.spring.user.User.id to com.hosting.hostinginterface.spring.user.User
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) ~[na:1.8.0_231]
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) ~[na:1.8.0_231]
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58) ~[na:1.8.0_231]
    at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36) ~[na:1.8.0_231]
    at java.lang.reflect.Field.get(Field.java:393) ~[na:1.8.0_231]
    at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:71) ~[hibernate-core-6.0.0.Alpha3.jar:6.0.0.Alpha3]
    ... 84 common frames omitted

这是我的实体类:

@Entity
public class User {

  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE)
  @Column(name = "id", updatable = false, nullable = false)
  private Long id;

  @Column(name = "username", nullable = false)
  private String userName;

  @Column(name = "firstName", nullable = false)
  private String firstName;

  @Column(name = "lastName", nullable = false)
  private String lastName;

  private LocalDate birthday;

  @Column(name = "birthdate", nullable = false)
  private String birthdayText;

  public User() {
  }

  @Contract(pure = true)
  public User(String userName, String firstName, String lastName, @NotNull LocalDate birthday) {
    this.userName = userName;
    this.firstName = firstName;
    this.lastName = lastName;
    this.birthday = birthday;
    birthdayText = birthday.toString();
  }

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public LocalDate getBirthday() {
    return birthday;
  }

  public void setBirthday(LocalDate birthday) {
    this.birthday = birthday;
  }

  public String getBirthdayText() {
    return birthdayText;
  }

  public void setBirthdayText(String birthdayText) {
    this.birthdayText = birthdayText;
  }

  public Long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }
}

更改 id 的生成方法似乎并不能解决错误,所以我没有可能的答案......

【问题讨论】:

  • 你的getter和setter都应该使用Long类型。
  • 我将设置器更改为 Long,但仍然出现相同的错误。
  • 休眠版本?
  • 我的版本是6.0.0 Alpha3
  • 这是休眠版本 4.1.9 更高版本中的错误(不确定)。将您的休眠版本更改为 4.1.9

标签: java hibernate jpa persistence


【解决方案1】:

试试这个id字段

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

数据库表中有birthday 列吗?我建议它应该有@Transient 不被保存到数据库

您可以尝试的另一件事是排除 spring-boot-devtools

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <optional>true</optional>
</dependency>

【讨论】:

  • 感谢您提醒我有关生日的事情 :) 不幸的是,更改生成类型后仍然会抛出异常
  • 您可以尝试的另一件事是排除spring-boot-devtools。我已经编辑了答案
  • 我也做了同样的事情,可惜没有成功。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-09
  • 1970-01-01
  • 2021-10-26
  • 2014-04-07
  • 1970-01-01
相关资源
最近更新 更多