0

When I try to save my entity in my oracle db using the .save() method of the hibernate session I get this exception :

org.hibernate.PropertyAccessException: Could not set field value [1757] value by reflection : [class mypackage.MyEntity.myid] setter of mypackage.MyEntity.myid

The value 1757 is correct, it's the one I want for my ID. (when I do a select myschema.mysequence from dual; in my DB I get the next value of this one so it seems to work properly) I don't understand the error, it's like it doesn't find the setter method but when I explore the exception, in the cause it says something else :

Can not set java.lang.Long field mypackage.MyEntity.myid to null value

which, for me, doesn't make any sense since the value "1757" is not null at all obviously ^^, and Long should accept null values anyway so I have no idea why it doesn't work. I supposed it was because an ID column coulnd't be null (which is false since we also can have a default for null values). So really, I don't get it.

My entity :

@Entity
@Table(name = "MYTABLE")
public class MyEntity implements Serializable{
    private static final long serialVersionUID = 1L;
    @Id
    @SequenceGenerator(name="seqName", sequenceName="myschema.mysequence",allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seqName") 
    @Column(name = "MY_ID")
    private Long myid; //NUMBER
    //some other column
    public Long getMyid() {
        return myid;
    }
    public void setMyid(Long myid) {
        this.myid = myid;
    }
    //some other accessors
}

I tried to change the type "Long" to long and I tried to do some other setters with different types (long, int, BigInteger, Integer, BigDecimal...) but nothing changed.

I'm able to create an occurrence using the "createNativeQuery" method with "myschema.mysequence.nextval" in myid field like this :

session.createNativeQuery("INSERT INTO myschema.MYTABLE (MY_ID, someotherfields)" 
    + " VALUES (myschema.mysequence.nextval, someotherfieldvalues)")
          .executeUpdate();

but if I do so, the id is lost... I want to get it back from the query like it's supposed to be with the session.save() method.

For now, the only way I found to get my ID from sequence after insert statement was to create it before (SELECT myschema.mysequence.nextval FROM DUAL) and it's terrible for the performances. We often add rows by thousands.

Gadou
  • 49
  • 1
  • 7

2 Answers2

0

Try adding a no-parameter constructor to your entity.

Omeri
  • 187
  • 2
  • 16
  • This will explain why: https://stackoverflow.com/questions/2935826/why-does-hibernate-require-no-argument-constructor – Omeri Aug 19 '21 at 22:37
  • 1
    There is no constructeur in my entity so by default, a no-parameter constructor exists, by the way if it was a constructor problem, I wouldnt be able to save it by any way, but here, only the sequence field doesn't work and if I get the sequence by my own way (select) it works perfectly well. I tried, regardless the arguments above and added "public MyEntity(){}" to my entity and it doesn't work better, exactly the same error (org.hibernate.PropertyAccessException: Could not set field value [2040] value by reflection : [class somepackages.myid] setter of somepackages.myid) – Gadou Aug 20 '21 at 08:06
  • Ok, another thing you could try is to turn on hibernate SQL logging and see what is going on. Could it be that the sequence in the database is not found or otherwise inaccessible? Have you possibly tried to use the sequence directly in the database via a query using the credentials used by your app? – Omeri Aug 26 '21 at 20:52
0

Did you try debugging to the point where the exception is initially thrown and inspect what is happening exactly? Maybe some underlying exception is swallowed. Are you sure you updated Hibernate to the latest version 5.4.34/5.5.7? If so, and you still have the problem, please create an issue in the issue tracker(https://hibernate.atlassian.net) with a test case(https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) that reproduces the issue.

Christian Beikov
  • 15,141
  • 2
  • 32
  • 58
  • Hi, thank you for your concern ! I use the 5.5.3.Final version. I'll try with the latest and if it still does not work I'll create an issue as you suggest. I always log debug in dev environment – Gadou Aug 27 '21 at 08:31