gpt4 book ai didi

java - Hibernate:java.lang.IllegalArgumentException:无法设置 int 字段

转载 作者:行者123 更新时间:2023-11-29 07:21:48 25 4
gpt4 key购买 nike

我正在尝试使用 hibernate 在我的 MySQL 数据库中插入一个条目。但我收到 IllegalArgumentException。id 是我数据库中的主键,我检查了自动增量选项。

enter image description here

org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [public int com.nexus.tutorial.Items.id] by reflection for persistent property [com.nexus.tutorial.Items#id] : Items [name=test, price=20.0, lagerstand=5, anfver=ankauf, position=0,0,0]
...
...
...
Caused by: java.lang.IllegalArgumentException: Can not set int field com.nexus.tutorial.Items.id to com.nexus.tutorial.Items
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at sun.reflect.UnsafeIntegerFieldAccessorImpl.getInt(UnsafeIntegerFieldAccessorImpl.java:56)
at java.lang.reflect.Field.getInt(Field.java:574)
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:62)
public class HibernateUpdate {

public static void update(Items item) {

SessionFactory factory = new Configuration()
.configure("C:\\Users\\N3XUS\\AppData\\\\Roaming\\.minecraft\\config\\hibernate.cfg.xml")
.addAnnotatedClass(Items.class)
.buildSessionFactory();

Session session = factory.getCurrentSession();

try {
System.out.println("Creating new Items");
session.beginTransaction();
//List<Items> itemList = session.createQuery("from Items").list();
session.save(item);
session.getTransaction().commit();

}finally {
factory.close();
}
}

}

这是我调用方法的方式:

HibernateUpdate.update(new Items("test", 20.00, 5,"ankauf","0,0,0"));

这是我的模型:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="items")
public class Items {

@Id
@Column(name="id")
public int id;

@Column(name="name")
public String name;

@Column(name="price")
public double price;

@Column(name="lagerstand")
public int lagerstand;

@Column(name="anfver")
public String anfver;

@Column(name="position")
public String position;

public Items() {

}

public Items(String name, double price, int lagerstand, String anfver, String position) {
this.name = name;
this.price = price;
this.lagerstand = lagerstand;
this.anfver = anfver;
this.position = position;
}

public int getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public int getLagerstand() {
return lagerstand;
}

public void setLagerstand(int lagerstand) {
this.lagerstand = lagerstand;
}

public String getAnfver() {
return anfver;
}

public void setAnfver(String anfver) {
this.anfver = anfver;
}

public String getPosition() {
return position;
}

public void setPosition(String position) {
this.position = position;
}

@Override
public String toString() {
return "Items [name=" + name + ", price=" + price + ", lagerstand=" + lagerstand + ", anfver=" + anfver
+ ", position=" + position + "]";
}

}

我知道修饰符应该是私有(private)的。我正在使用来自 maven-repository 的 JDBC 和 hibernate 的最新版本。

    compile 'org.hibernate:hibernate-agroal:5.3.10.Final'
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.15'

最佳答案

您必须使用 GeneratedValue 来确保您的 id 是自动递增的:

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
public int id;

在此之后您的 ID 将自动递增,您可以添加您的项目:

HibernateUpdate.update(new Items("test", 20.00, 5,"ankauf","0,0,0"));

Advice: your attributes in preference must be private or protected and from getter and setter you can access to them.

关于java - Hibernate:java.lang.IllegalArgumentException:无法设置 int 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55956513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com