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: 'Attempt to insert duplicate key row
PostPosted: Tue May 12, 2009 6:28 pm 
Newbie

Joined: Wed Jan 28, 2009 12:45 pm
Posts: 2
I get 'Attempt to insert duplicate key row in object' error when my application tries to insert a row. Only way I could get rid of the error is by taking out hashcode and equal method. I tried implementing hash code and equal method different ways (hashCode builder, eclipse generated etc) but no help. I am copying my java file, xml mapping file file and colsole messages. Any help will be greatly appreciated.

Java file--------
Code:
import java.util.Set;
import org.hibernate.proxy.HibernateProxyHelper;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

public class Server implements IBusinessObject
{
   private static final long serialVersionUID = 1L;   
             private Integer serverID;
             private String  name;
             private String  description;   
     
    public Server()
    {
        serverID = new Integer(Constants.UNDEFINED);
    }

    public boolean equals(Object obj) {   
       if (obj instanceof Server) {       
          Server other = (Server) obj;       
          EqualsBuilder builder = new EqualsBuilder();
          builder.append(getName(), other.getName());       
          return builder.isEquals();   
       }   
       return false;
    }
    public int hashCode() {   
       HashCodeBuilder builder = new HashCodeBuilder();
       builder.append(getName());   
       return builder.toHashCode();
    } 

    public String toString()
    {
        return new ToStringBuilder(this).append("name", this.name)
                .append("description", this.description)
                .toString();
    }

    public Integer getServerID() {
      return serverID;
   }

    public void setServerID(Integer serverID) {
      this.serverID = serverID;
}

   
    public String getName()
    {
        return name;
    }


    public void setName(String name)
    {
        this.name = name;
    }

   public String getDescription() {
      return description;
   }

   public void setDescription(String description) {
      this.description = description;
   }   
}


--------------

Server.hbm.xml
---------------
Code:
<hibernate-mapping>
   <class
      name="com.businessObject.Server"
      table="server">

      <id name="serverID" column="server_id"
         type="java.lang.Integer" unsaved-value="-1">
         <generator class="increment"></generator>
      </id>

      <property name="name" type="java.lang.String" column="name"
         not-null="true" unique="true"/>
         
      <property name="description" type="java.lang.String" column="description"
         not-null="false" unique="false"/>
   </class>

</hibernate-mapping>


Console---------------
12-05-2009 13:35:54 [http-8080-Processor3] action.ServerAction (DEBUG): in server save
12-05-2009 13:35:54 [http-8080-Processor5] hibernate.ServerDAOHibernate (DEBUG): HashCode-1239849451
12-05-2009 13:35:54 [http-8080-Processor5] hibernate.SQL (DEBUG): insert into server (name, description, server_id) values (?, ?, ?)
Hibernate: insert into server (name, description, server_id) values (?, ?, ?)
12-05-2009 13:35:54 [http-8080-Processor5] type.StringType (DEBUG): binding '11-myserver-1' to parameter: 1
12-05-2009 13:35:54 [http-8080-Processor5] type.StringType (DEBUG): binding '11-myserver-1' to parameter: 2
12-05-2009 13:35:54 [http-8080-Processor5] type.IntegerType (DEBUG): binding '113' to parameter: 3
12-05-2009 13:35:54 [http-8080-Processor3] hibernate.ServerDAOHibernate (DEBUG): HashCode-1239849451
12-05-2009 13:35:54 [http-8080-Processor3] hibernate.SQL (DEBUG): insert into server (name, description, server_id) values (?, ?, ?)
Hibernate: insert into server (name, description, server_id) values (?, ?, ?)
12-05-2009 13:35:54 [http-8080-Processor3] type.StringType (DEBUG): binding '11-myserver-1' to parameter: 1
12-05-2009 13:35:54 [http-8080-Processor3] type.StringType (DEBUG): binding '11-myserver-1' to parameter: 2
12-05-2009 13:35:54 [http-8080-Processor3] type.IntegerType (DEBUG): binding '114' to parameter: 3
12-05-2009 13:35:54 [http-8080-Processor3] util.JDBCExceptionReporter (ERROR): Attempt to insert duplicate key row in object 'server' with unique index 'server_2031447382'


Top
 Profile  
 
 Post subject: Re: 'Attempt to insert duplicate key row
PostPosted: Wed May 13, 2009 5:51 am 
Newbie

Joined: Tue May 12, 2009 2:53 am
Posts: 8
I think that, this is not the problem with either "equals" and "hashcode" method.

you have defined "name" as unique in the table "server". check in the table "server" whether it already has the same value in the "name" column that you are trying to update.

The exception says that "unque constraint violated".


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.