1

Im Trying to insert an entry in my MySQL-database using hibernate. But I'm getting an IllegalArgumentException. The id is a primary key in my database and I checked the autoincrement option.

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();
        }
    }

}

this is how I call the method:

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

and this is my Model:

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 + "]";
    }

}

The modifier should be private I know. I'm using the latest versions for JDBC and hibernate from the maven-repository.

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

1 Answer 1

1

You must use GeneratedValue to ensure that your id is auto increment :

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

after this your ID is auto increment and you can add your item :

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I tryed this out but I still get the same Error. I also tryed diffrent generationstypes. But I don't think that this is the problem because I have an example progrogramm using hibernate and the Id and Column Annotation was allways enough to autogenerate and increment IDs.
Oh wait thats not true. The Error is a bit diffrent now not the id but the anfver field has this error: rg.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.String com.nexus.tutorial.Items.anfver] by reflection for persistent property [com.nexus.tutorial.Items#anfver] : Items [name=name, price=20.0, lagerstand=50, anfver=test, position=test] caused by Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field com.nexus.tutorial.Items.anfver to com.nexus.tutorial.Items
Try returnig this.anfver in your getter public String getAnfver() { return this.anfver; }
Thats a diffrent problem
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.