1. ActiveJDBC 1.4.9
2. SQL Server 2012
I have a very simple table (called Updates) in which I track the last update times for a few components. It has 2 columns - "Component", a varchar(50) which is the primary key, and "LastUpdate" a datetime.
When I try to use ActiveJDBC to update the "LastUpdate" column of a row, it seems to be doing an insert, which fails due to primary key constraint.
Update update = Update.findFirst("Component = ?", "Rally");
if (update != null)
{
logger.debug("Found existing record in Updates table");
update.set("LastUpdate", iso8601.format(currentTime)).saveIt();
}
2014-05-16 11:13:02 DEBUG RallyToSQLServer:132 - Found existing record in Updates table
Exception in thread "main" org.javalite.activejdbc.DBException: com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constraint 'PK_Updates'. Cannot insert duplicate key in object 'dbo.Updates'. The duplicate key value is (Rally)., Query: INSERT INTO updates (lastupdate, component) VALUES (?, ?), params: 2014-05-16T18:13:02Z,Rally
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1515)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:404)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:314)
at org.javalite.activejdbc.DB.execInsert(DB.java:551)
at org.javalite.activejdbc.Model.doInsert(Model.java:2160)
at org.javalite.activejdbc.Model.save(Model.java:2067)
at com.intel.spd.rally.models.db.Update.save(Update.java)
at org.javalite.activejdbc.Model.saveIt(Model.java:1994)
at com.intel.spd.rally.models.db.Update.saveIt(Update.java)
at com.intel.spd.rally.RallyToSQLServer.run(RallyToSQLServer.java:134)
at com.intel.spd.rally.RallyToSQLServer.main(RallyToSQLServer.java:105)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constraint 'PK_Updates'. Cannot insert duplicate key in object 'dbo.Updates'. The duplicate key value is (Rally).
... 17 more