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'