3

我是hibernate的新手,我有以下课程,我使用的是Spring hibernate和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;
}

下面提到的是我们数据库的结构,

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       |                |
+-----------------------------+---------------+------+-----+---------+----------------+

每当我尝试将记录插入设备表时,都会出现以下错误,

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)

我真的不知道为什么会出现上述错误。

所有设置器都已正确安装,已将其删除,只是为了避免更多代码。

表之间的关系如下,DeviceType 定义了设备的类型。Device 表定义了实际的设备,因此 DeviceType 是 Device 的属性之一,Many 设备会有相同的 DeviceType(遵循 ManyToOne 的原则,Many Devices 有相同的 DeviceType)。

4

1 回答 1

2

在 Hibernate 中,您映射实体,并在对象世界中描述那里的关系,而不是在数据库世界中。因此,您映射:

@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 足够聪明,可以将这种关系映射到数据库世界中,它成为一个短外键

于 2014-08-03T13:17:58.047 回答