-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: PK violation error on update
PostPosted: Wed Dec 23, 2015 1:08 am 
Newbie

Joined: Tue Dec 22, 2015 11:51 pm
Posts: 2
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.


Top
 Profile  
 
 Post subject: Re: PK violation error on update
PostPosted: Sat Dec 26, 2015 1:30 am 
Newbie

Joined: Tue Dec 22, 2015 11:51 pm
Posts: 2
The problem has been solved. I had a wrong reference deep in the Java code to a wrong method. Now it is fixed and it works.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.