2013-11-21 57 views
0

我使用Hibernate 3.6.0與MySQL 5.5.15。我有一個具有@GeneratedValue ID字段的實體。當我嘗試堅持實體時,它會拋出異常:沒有爲參數5指定值。第五個參數是ID字段。我該如何解決這個問題?獲得「值java.sql.SQLException:沒有爲參數指定值」爲@GeneratedValue場

實體類:

@Entity 
@Table(name = "T_DEVICE_HISTORY") 
public class DeviceHistory { 

    @Id 
    @Column(name = "ID") 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private long id; 

    @Column(name = "DEVICE_ID") 
    private String deviceId; 


    @Column(name = "GUID", length = 36) 
    private String guid; 

    @Column(name = "UPDATE_DATE") 
    private Date updated; 

    @Column(name = "STATUS", nullable = false) 
    @Enumerated(EnumType.STRING) 
    private DeviceStatus status; 

    // getters,setters ... 

    @PrePersist 
    public void setInitialState() { 
     setUpdated(new Date()); 
    } 

} 

代碼持續實體:

DeviceHistory deviceHistory = new DeviceHistory(); 
deviceHistory.setGuid(guid); 
deviceHistory.setDeviceId(deviceId); 
deviceHistory.setStatus(DeviceStatus.INACTIVE); 
entityManager.persist(deviceHistory); 

堆棧跟蹤:

06:12:58,123 ERROR [JDBCExceptionReporter] No value specified for parameter 5 
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not insert: [com.evolok.idm.core.domain.DeviceHistory] 
    at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:630) 
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:219) 
    at com.evolok.idm.core.domain.RestrictedUnregisterPerDurationDeviceManagementPolicyDelegate.unregisterDevice(RestrictedUnregisterPerDurationDeviceManagementPolicyDelegate.java:78) 
    at com.evolok.idm.core.domain.DeviceManagementPolicyEntity.unregisterDevice(DeviceManagementPolicyEntity.java:86) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:157) 
    at com.evolok.idm.core.domain.DeviceManagementPolicyEntity$$EnhancerByCGLIB$$2ff84b00.unregisterDevice(<generated>) 
    at com.evolok.idm.services.core.UserProfileComponent.unregisterDevice(UserProfileComponent.java:479) 
    at com.evolok.idm.notification.proxies.UserProfileServiceNotificationProxy.unregisterDevice(UserProfileServiceNotificationProxy.java:186) 
    at com.evolok.idm.services.web.endpoints.rest.UserProfileResource.unregisterDevice(UserProfileResource.java:567) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.sca4j.pojo.component.InvokerInterceptor.invoke(InvokerInterceptor.java:192) 
    at org.sca4j.pojo.component.InvokerInterceptor.invoke(InvokerInterceptor.java:165) 
    at org.sca4j.fabric.component.scope.RequestScopeInterceptor.invoke(RequestScopeInterceptor.java:40) 
    at org.sca4j.rs.runtime.RsSourceWireAttacher$RsMethodInterceptor.intercept(RsSourceWireAttacher.java:163) 
    at com.evolok.idm.services.web.endpoints.rest.UserProfileResource$$EnhancerByCGLIB$$99ca8d93.unregisterDevice(<generated>) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:156) 
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67) 
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:163) 
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:111) 
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:71) 
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:111) 
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:63) 
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:654) 
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:612) 
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:603) 
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:309) 
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:425) 
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:590) 
    at org.sca4j.rs.runtime.rs.RsWebApplication.service(RsWebApplication.java:148) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) 
    at org.sca4j.runtime.webapp.ServletHostImpl.service(ServletHostImpl.java:138) 
    at org.sca4j.runtime.webapp.SCA4JServlet.service(SCA4JServlet.java:85) 
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:534) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1351) 
    at com.evolok.idm.web.TransactionFilter.doFilter(TransactionFilter.java:47) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1322) 
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:473) 
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119) 
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:516) 
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226) 
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:929) 
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:403) 
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:184) 
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:864) 
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117) 
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:247) 
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:151) 
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:114) 
    at org.eclipse.jetty.server.Server.handle(Server.java:352) 
    at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:596) 
    at org.eclipse.jetty.server.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:1051) 
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:590) 
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:212) 
    at org.eclipse.jetty.server.HttpConnection.handle(HttpConnection.java:426) 
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:508) 
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.access$000(SelectChannelEndPoint.java:34) 
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:40) 
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:451) 
    at java.lang.Thread.run(Thread.java:695) 
Caused by: org.hibernate.exception.SQLGrammarException: could not insert: [com.evolok.idm.core.domain.DeviceHistory] 
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67) 
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) 
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40) 
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158) 
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638) 
    at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48) 
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250) 
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298) 
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181) 
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107) 
    at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:49) 
    at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:131) 
    at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:87) 
    at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38) 
    at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:618) 
    at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:592) 
    at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:596) 
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:213) 
    ... 68 more 
Caused by: java.sql.SQLException: No value specified for parameter 5 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926) 
    at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2176) 
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1993) 
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937) 
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922) 
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73) 
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33) 
    ... 83 more 

生成的語句:

insert into T_DEVICE_HISTORY (DEVICE_ID, GUID, STATUS, UPDATE_DATE, ID) values (?, ?, ?, ?, ?) 

編輯:表結構:

CREATE TABLE `T_DEVICE_HISTORY` (
    `ID` bigint(20) NOT NULL AUTO_INCREMENT, 
    `DEVICE_ID` varchar(255) DEFAULT NULL, 
    `GUID` varchar(36) DEFAULT NULL, 
    `STATUS` varchar(255) NOT NULL, 
    `UPDATE_DATE` datetime DEFAULT NULL, 
    PRIMARY KEY (`ID`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

編輯2 - 的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
      version="1.0"> 

    <persistence-unit name="evolok-idm" transaction-type="RESOURCE_LOCAL"> 
     <provider>org.hibernate.ejb.HibernatePersistence</provider> 

     <class>com.evolok.idm.core.domain.DeviceHistory</class> 
     <!-- other classes... --> 

     <exclude-unlisted-classes>true</exclude-unlisted-classes> 

     <properties> 
      <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/evolok-idm-web"/> 
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> 
      <property name="hibernate.connection.username" value="sa"/> 
      <property name="hibernate.connection.password" value=""/> 
      <property name="hibernate.connection.autocommit" value="false"/> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/> 
      <property name="hibernate.connection.isolation" value="4"/> 
      <property name="hibernate.show_sql" value="true"/> 
      <property name="hibernate.format_sql" value="false"/> 
     </properties> 

    </persistence-unit> 

</persistence> 
+0

你的id字段中是否有自動增量? – erencan

+0

是的,當我檢查表結構時,該字段是:'ID bigint(20)NOT NULL AUTO_INCREMENT' –

回答

0

從你的代碼,我可以看到updateDate丟失...是達可空場?

AUTO是默認策略,因此您可以刪除。並嘗試以下等效定義。

@Id 
    @Column(name = "ID") 
    @GeneratedValue 
    private long id; 
+0

用表結構更新了文章。 –

+0

你的意思是我應該刪除@Id和@Column,只留下@GeneratedValue?這在啓動時會出現異常:'org.hibernate.AnnotationException:沒有爲實體指定標識符:com.evolok.idm.core.domain.DeviceHistory' –

+0

我不是那個意思。這是我的錯字..我更新了帖子..顯然,當實體沒有ID時,我們會得到沒有爲實體錯誤指定的標識符.. – BDR

0

更改您的實體,如下所示,您需要添加默認構造函數。

@Entity 
@Table(name = "T_DEVICE_HISTORY") 
    public class DeviceHistory implements java.io.Serializable { 

     public DeviceHistory(){ 
      super(); 
      id = null ; 
     } 


     @Id 
     @Column(name = "ID") 
     @GeneratedValue(strategy=GenerationType.AUTO) 
     private long id; 
+0

我的領域是原始類型,所以不能在構造函數中分配null。可能有一個原始類型會導致問題?還有,如何添加默認的構造函數幫助?如果我沒有指定任何構造函數,則默認情況下執行完全相同的操作,調用super()。 –

相關問題