I reverse engineered SQL Server 2014 database to Hibernate using JBossTools4.3.0. The queries for fetch and save run smoothly but when I want to
update an attribute in a single row of db, I am getting a SQL Exception:
Quote:
com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constraint 'PK_Project'. Cannot insert duplicate key in object 'dbo.Project'. The duplicate key value is (AMD-123)
The update attempts to change a boolean value in one cell (not PK) in a row identified by AMD-123 (as projectId) in PK column. I tried this update in three update versions: regular Hibernate update(...) and saveOrUpdate(..), another using
Code:
org.hibernate.Query query = session.createQuery("UPDATE Project SET proIsActive = :proIsActive"
+ " WHERE projectId =:projectId ");
query.setParameter("proIsActive", project.isProIsActive());
query.setParameter("projectId", project.getProjectId());
and
Code:
session.doWork(new Work() {
@Override
public void execute(java.sql.Connection connection) throws SQLException {
java.sql.PreparedStatement ps;
ps = connection.prepareStatement("UPDATE Project SET ProIsActive = ? WHERE ProjectID = ?");
ps.setString(1, project.getProjectId());
ps.setBoolean(2, project.isProIsActive());
All gave the same exception.
I am converting .NET windows project to Java EE Web application. All queries including
update work fine with C# and SQL Server. Actually, I am using stored procedures for all db operations in that windows application but not with Hibernate. I hope that Hiberanate will give me a db independent format without the use of stored procedures.
I have searched for possible solutions on the web and this violation have shown up many times for the past 10 years or so but I have not found any remedy for my problem.
I would appreciate your help in solving this problem.