2017-06-20 49 views
0

我有一個運行這樣的代碼:的EclipseLink MySQLSyntaxErrorException找不到錯誤

S3Logs s3Logs = new S3Logs(); 
          s3Logs.setBucketOwner(a.getBucketOwner()); 
          s3Logs.setBucket(a.getBucket()); 
          s3Logs.setTime(a.getTime().toString()); 
          s3Logs.setRemoteIpAddress(a.getRemoteIpAddress()); 
          s3Logs.setRequester(a.getRequester()); 
          s3Logs.setRequestId(a.getRequestId()); 
          s3Logs.setOperation(a.getOperation()); 
          s3Logs.setKey(a.getKey()); 
          s3Logs.setRequestUri(a.getRequestUri()); 
          s3Logs.setHttpStatus(a.getHttpStatus()); 
          s3Logs.setErrorCode(a.getErrorCode()); 
          s3Logs.setBytesSent(a.getBytesSent()); 
          s3Logs.setObjectSize(a.getObjectSize()); 
          s3Logs.setTotalTime(a.getTotalTime()); 
          s3Logs.setTurnAroundTime(a.getTurnAroundTime()); 
          if(a.getReferrer() == "-"){ 
           s3Logs.setReferrer(null); 
          }else{ 
          s3Logs.setReferrer(a.getReferrer());} 
          if(a.getUserAgent().contains(",")){ 
           String s = a.getUserAgent().replace(",", ""); 
           s3Logs.setUserAgent(s); 
          }else{ 
           s3Logs.setUserAgent(a.getUserAgent()); 
          } 

          s3Logs.setVersionId(a.getVersionId()); 
          LogsDAO.em.persist(s3Logs); 
          LogsDAO.em.getTransaction().commit(); 

但是,當我試圖運行,它給了我的錯誤,像這樣:

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'KEY, OBJECTSIZE, OPERATION, REFERRER, REMOTEIPADDRESS, REQUESTID, REQUESTURI, RE' at line 1 
Error Code: 1064 
Call: INSERT INTO S3LOGS (BUCKET, BUCKETOWNER, BYTESSENT, ERRORCODE, EXTRAS, HTTPSTATUS, KEY, OBJECTSIZE, OPERATION, REFERRER, REMOTEIPADDRESS, REQUESTID, REQUESTURI, REQUESTER, TIME, TOTALTIME, TURNAROUNDTIME, USERAGENT, VERSIONID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 
    bind => [seminar2017, 5abc648552c5f, 0, null, null, 200, null, 0, REST.PUT.LOGGING_STATUS, -, 10.0.0.100, C785888F31E34747, PUT /seminar2017?logging= HTTP/1.1, 5abc648552c5f, 2017-05-10T15:42:40.000Z, 198, 0, aws-internal/3 S3Console/0.4, null] 
Query: InsertObjectQuery([email protected]) 
Exception in thread "main" javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): org.eclipse.persistence.exceptions.DatabaseException 
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'KEY, OBJECTSIZE, OPERATION, REFERRER, REMOTEIPADDRESS, REQUESTID, REQUESTURI, RE' at line 1 
Error Code: 1064 
Call: INSERT INTO S3LOGS (BUCKET, BUCKETOWNER, BYTESSENT, ERRORCODE, EXTRAS, HTTPSTATUS, KEY, OBJECTSIZE, OPERATION, REFERRER, REMOTEIPADDRESS, REQUESTID, REQUESTURI, REQUESTER, TIME, TOTALTIME, TURNAROUNDTIME, USERAGENT, VERSIONID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 
    bind => [cpabecloudseminar2017, 5abc648552c5f, 0, null, null, 200, null, 0, REST.PUT.LOGGING_STATUS, -, 54.239.6.71, C785888F31E34747, PUT /seminar2017?logging= HTTP/1.1, 5abc648552c5f, 2017-05-10T15:42:40.000Z, 198, 0, aws-internal/3 S3Console/0.4, null] 
Query: InsertObjectQuery([email protected]) 
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:159) 
    at de.awsmonitor.AWSLogsMapper.main(AWSLogsMapper.java:176) 
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): org.eclipse.persistence.exceptions.DatabaseException 
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'KEY, OBJECTSIZE, OPERATION, REFERRER, REMOTEIPADDRESS, REQUESTID, REQUESTURI, RE' at line 1 
Error Code: 1064 
Call: INSERT INTO S3LOGS (BUCKET, BUCKETOWNER, BYTESSENT, ERRORCODE, EXTRAS, HTTPSTATUS, KEY, OBJECTSIZE, OPERATION, REFERRER, REMOTEIPADDRESS, REQUESTID, REQUESTURI, REQUESTER, TIME, TOTALTIME, TURNAROUNDTIME, USERAGENT, VERSIONID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 
    bind => [seminar2017, 5abc648552c5f291cc61eb5cbcb347c3074ed816d2a8bd140e598b3e30595338, 0, null, null, 200, null, 0, REST.PUT.LOGGING_STATUS, -, 10.0.0.100, C785888F31E34747, PUT /seminar2017?logging= HTTP/1.1, 5abc648552c5f291cc61eb5cbcb347c3074ed816d2a8bd140e598b3e30595338, 2017-05-10T15:42:40.000Z, 198, 0, aws-internal/3 S3Console/0.4, null] 
Query: InsertObjectQuery([email protected]) 
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:331) 
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:902) 
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeNoSelect(DatabaseAccessor.java:964) 
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:633) 
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:560) 
    at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:2056) 
    at org.eclipse.persistence.sessions.server.ClientSession.executeCall(ClientSession.java:306) 
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:242) 
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:228) 
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.insertObject(DatasourceCallQueryMechanism.java:377) 
    at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:165) 
    at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:180) 
    at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.insertObjectForWrite(DatabaseQueryMechanism.java:489) 
    at org.eclipse.persistence.queries.InsertObjectQuery.executeCommit(InsertObjectQuery.java:80) 
    at org.eclipse.persistence.queries.InsertObjectQuery.executeCommitWithChangeSet(InsertObjectQuery.java:90) 
    at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.executeWriteWithChangeSet(DatabaseQueryMechanism.java:301) 
    at org.eclipse.persistence.queries.WriteObjectQuery.executeDatabaseQuery(WriteObjectQuery.java:58) 
    at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:904) 
    at org.eclipse.persistence.queries.DatabaseQuery.executeInUnitOfWork(DatabaseQuery.java:803) 
    at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWorkObjectLevelModifyQuery(ObjectLevelModifyQuery.java:108) 
    at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWork(ObjectLevelModifyQuery.java:85) 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896) 
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1857) 
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1839) 
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1790) 
    at org.eclipse.persistence.internal.sessions.CommitManager.commitNewObjectsForClassWithChangeSet(CommitManager.java:227) 
    at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsWithChangeSet(CommitManager.java:126) 
    at org.eclipse.persistence.internal.sessions.AbstractSession.writeAllObjectsWithChangeSet(AbstractSession.java:4264) 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabase(UnitOfWorkImpl.java:1441) 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1531) 
    at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitRootUnitOfWork(RepeatableWriteUnitOfWork.java:278) 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitAndResume(UnitOfWorkImpl.java:1169) 
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:134) 
    ... 1 more 
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'KEY, OBJECTSIZE, OPERATION, REFERRER, REMOTEIPADDRESS, REQUESTID, REQUESTURI, RE' at line 1 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) 
    at com.mysql.jdbc.Util.getInstance(Util.java:408) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2677) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549) 
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861) 
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073) 
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009) 
    at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5098) 
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994) 
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:892) 
    ... 32 more 

我試着找出錯誤,但直到現在才知道。我甚至試圖實例化S3Log對象而不放入任何東西,但它仍然給出了相同的錯誤。

這裏是我的S3Log對象:

@Entity 
public class S3Logs { 

    public S3Logs() { 

    } 

    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE) 
    public long id; 

    private String bucketOwner; 
    private String bucket; 
    private String time; 
    private String remoteIpAddress; 
    private String requester; 
    private String requestId; 
    private String operation; 
    private String key; 
    private String requestUri; 
    private int httpStatus; 
    private String errorCode; 
    private long bytesSent; 
    private long objectSize; 
    private long totalTime; 
    private long turnAroundTime; 
    private String referrer; 
    private String userAgent; 
    private String versionId; 
} 
+3

密鑰是保留字。在查詢時用反引號將其撤銷,最好儘量避免在保留的SQL關鍵字等數據庫中命名錶/列。 –

+0

或使用JPA提供程序來爲您做到這一點,所以您不必妥協...... –

回答

3

的問題

MySQL是給你一個有關問題的所在,這裏提示:

錯誤代碼:1064

您的SQL語法有錯誤;檢查對應於你的MySQL服務器版本的手冊正確的語法附近

'KEY, OBJECTSIZE, OPERATION, REFERRER, REMOTEIPADDRESS, REQUESTID, REQUESTURI, RE' at line 1

KEY使用是一個保留字在MySQL。並且保留字不能用作標識符(例如表名,列名)

要使用保留字作爲標識符(或使用包含無效字符(如空格)的字符串),標識符可以通過將它包含在反引號字符中來逃脫。

例如:

INSERT INTO `mytable` (`mycol`, `key`, ... 
      ^ ^^ ^^ ^

THE FIX

一種快速的方法來解決這一問題是增加一個JPA 2.0指令中的源極,覆蓋默認的列名,指定反引號中包含的列名如下所示:

private String operation; 
@Column(name="`key`") 
private String key; 
private String requestUri; 

將列名更改爲保留字以外的內容將是另一個(首選)選項。

有些人會(正確地)爭辯說這是一個MySQL特定的修補程序。 (MySQL使用單向反引號,SQL Server使用方括號來轉義標識符,Oracle使用雙引號等等)。所以這個「修復」在數據庫可移植性方面飛速發展。

在JPA配置中應該有一些常規設置,它有效地表示「轉義所有標識符」,這不會是數據庫特定的。

+0

JPA規範定義了使用@Column(name =「\」key \「」)(JPA 2。0,第2.13節),以按名稱使用分隔符。這將轉換爲數據庫特定的表示法,並且使用變通辦法可能會導致本機查詢出現問題,其中返回的列名可能與列定義不匹配。 – Chris