java - could not set a field value by reflection setter of <class> -
have seen similar question in forum, here mapping value in junction table & hibernate.propertyaccessexception: not set field value reflection setter
i've applied solution mentioned in question still facing same problem. don't know missing. classes -
employeeid.java
@idclass(value = employeeid.class) public class employeeid implements serializable{ private int empid; private int deptid; public employeeid() { } public employeeid(int empid, int deptid) { this.empid = empid; this.deptid = deptid; } public int getempid() { return empid; } public int getdeptid() { return deptid; } @override public int hashcode() { final int prime = 31; int result = 1; result = prime * result + deptid; result = prime * result + empid; return result; } @override public boolean equals(object obj) { if (this == obj) return true; if (obj == null) return false; if (getclass() != obj.getclass()) return false; employeeid other = (employeeid) obj; if (deptid != other.deptid) return false; if (empid != other.empid) return false; return true; } }
employee.java
@entity(name="employee") @idclass(value = employeeid.class) public class employee { @id @generatedvalue @column(name="emp_id") private int empid; @id @generatedvalue @column(name="dept_id") private int deptid; public int getempid() { return empid; } public void setempid(int empid) { this.empid = empid; } public int getdeptid() { return deptid; } public void setdeptid(int deptid) { this.deptid = deptid; } @column(name="emp_name") private string empname; public string getempname() { return empname; } public void setempname(string empname) { this.empname = empname; } }
and test code -
try { employee emp = new employee(); emp.setempname("new employee"); session session = factory.opensession(); session.begintransaction(); session.save(emp); session.gettransaction().commit(); session.close(); } catch (exception e) { e.printstacktrace(); } { factory.close(); }
on running test code exception trace -
org.hibernate.propertyaccessexception: not set field value reflection setter of employeeid.deptid @ org.hibernate.property.directpropertyaccessor$directsetter.set(directpropertyaccessor.java:134) @ org.hibernate.mapping.component$valuegenerationplan.execute(component.java:441) @ org.hibernate.id.compositenestedgeneratedvaluegenerator.generate(compositenestedgeneratedvaluegenerator.java:121) @ org.hibernate.event.internal.abstractsaveeventlistener.savewithgeneratedid(abstractsaveeventlistener.java:117) @ org.hibernate.event.internal.defaultsaveorupdateeventlistener.savewithgeneratedorrequestedid(defaultsaveorupdateeventlistener.java:209) @ org.hibernate.event.internal.defaultsaveeventlistener.savewithgeneratedorrequestedid(defaultsaveeventlistener.java:55) @ org.hibernate.event.internal.defaultsaveorupdateeventlistener.entityistransient(defaultsaveorupdateeventlistener.java:194) @ org.hibernate.event.internal.defaultsaveeventlistener.performsaveorupdate(defaultsaveeventlistener.java:49) @ org.hibernate.event.internal.defaultsaveorupdateeventlistener.onsaveorupdate(defaultsaveorupdateeventlistener.java:90) @ org.hibernate.internal.sessionimpl.firesave(sessionimpl.java:715) @ org.hibernate.internal.sessionimpl.save(sessionimpl.java:707) @ org.hibernate.internal.sessionimpl.save(sessionimpl.java:702) @ compositekeydemo.main(compositekeydemo.java:15) caused by: java.lang.illegalargumentexception: can not set int field employeeid.deptid org.hibernate.id.identifiergeneratorhelper$2 @ sun.reflect.unsafefieldaccessorimpl.throwsetillegalargumentexception(unsafefieldaccessorimpl.java:164) @ sun.reflect.unsafefieldaccessorimpl.throwsetillegalargumentexception(unsafefieldaccessorimpl.java:168) @ sun.reflect.unsafeintegerfieldaccessorimpl.set(unsafeintegerfieldaccessorimpl.java:98) @ java.lang.reflect.field.set(field.java:741) @ org.hibernate.property.directpropertyaccessor$directsetter.set(directpropertyaccessor.java:122) ... 12 more
am doing silly.
in advance !!
i don't think can use @generatedvalue @idclass.