持久保存实体对象时休眠IllegalArgumentException:无法将字段'id'设置为实体对象吗?

问题描述 投票:0回答:1

我尝试保留一个实体对象,但收到一个讨厌的IllegalArgumentException。查看stacktrace并没有给我太多信息。休眠会以某种方式尝试将实体对象的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的生成方法似乎无法解决该错误,因此我没有可能的答案...

java hibernate jpa persistence
1个回答
0
投票

对于id字段尝试此操作

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
© www.soinside.com 2019 - 2024. All rights reserved.