java.lang.IllegalArgumentException: Can not set java.lang.Short field to java.lang.Short

Gokul Kulkarni picture Gokul Kulkarni · Aug 3, 2014 · Viewed 7.8k times · Source

I am new to hibernate, I am having the following classes, I am using Spring hibernate and mysql.

import javax.persistence.Entity;

@Entity
@Table(name = "Device")
public class DeviceForm {

@Id
@Column(name = "deviceid")
Integer deviceid;

@ManyToOne(targetEntity = DeviceType.class)
@JoinColumn(name = "devicetype_devicetypeid")
Short devicetypeid;

public Short getDevicetypeid() {
    return devicetypeid;
}

public void setDevicetypeid(Short devicetypeid) {
    this.devicetypeid = devicetypeid;
}

@Column(name = "title")
String title; // activity title

@Column(name = "description")
Blob description;

@Transient
String actdesc;

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

@Column(name = "duration")
short duration;

@Column(name = "memberscount")
Integer memberscount;
}


import javax.persistence.Entity;

@Entity
@Table(name="DeviceType")
public class DeviceType {

    @Id
    @Column(name = "devicetypeid")
    Short devicetypeid;

    @Column(name = "type")
    String type;
}

Below mentioned is the structure of our DB,

DeviceType
+----------------+-------------+------+-----+---------+-------+
| Field          | Type        | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| devicetypeid   | smallint(6) | NO   | PRI | NULL    |       |
| type           | varchar(45) | YES  |     | NULL    |       |
+----------------+-------------+------+-----+---------+-------+


Device
+-----------------------------+---------------+------+-----+---------+----------------+
| Field                       | Type          | Null | Key | Default | Extra          |
+-----------------------------+---------------+------+-----+---------+----------------+
| deviceid                    | int(11)       | NO   | PRI | NULL    | auto_increment |
| title                       | varchar(255)  | YES  |     | NULL    |                |
| description                 | blob          | YES  |     | NULL    |                |
| devicetype_devicetypeid     | smallint(6)   | NO   | MUL | NULL    |                |
| url                         | varchar(1024) | YES  |     | NULL    |                |
| duration                    | smallint(6)   | YES  |     | 1       |                |
| memberscount                | mediumint(9)  | YES  |     | 1       |                |
+-----------------------------+---------------+------+-----+---------+----------------+

Whenever I try inserting a record into the Device table I get the following error,

java.lang.IllegalArgumentException: Can not set java.lang.Short field DeviceType.devicetypeid to java.lang.Short
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(Unknown Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(Unknown Source)
at java.lang.reflect.Field.get(Unknown Source)
at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:55)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:206)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3619)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3335)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:204)
at org.hibernate.engine.ForeignKeys$Nullifier.isNullifiable(ForeignKeys.java:160)
at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:92)
at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:70)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:311)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:144)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:562)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:550)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:546)
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:686)
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:683)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683)
at com.educcess.dao.activity.impl.ActivityDaoImpl.createActivity(ActivityDaoImpl.java:15)
at com.educcess.service.activity.impl.ActivityServiceImpl.createActivity(ActivityServiceImpl.java:26)
at com.educcess.controller.activity.ActivityController.putActivity(ActivityController.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:822)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1721)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1679)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

I really have no clue why is I am getting the above error.

All the setters are in place properly, have removed those, just to avoid more code.

The relation between the tables is as follows, DeviceType defines the type of device. Device table defines the actual device, hence DeviceType is one of the property of Device, and Many device will have same DeviceType (following the principle of ManyToOne, Many Devices have same DeviceType).

Answer

Ralph picture Ralph · Aug 3, 2014

In Hibernate you map entities, and you describe there relationship in the object world, but not in the database world. Therefore you map:

@Entity
@Table(name = "Device")
public class DeviceForm {

    ...

    @ManyToOne(targetEntity = DeviceType.class)
    @JoinColumn(name = "devicetype_devicetypeid")
    DeviceType devicetype;

    //Getter and Setter with DeviceType but not Short

    ...
}

Hibernate is smart enough to map this relation in the Database world where it become a Short Foreign Key