I got exception: duplicate key value violates unique constraint “client_pkey”
Key (xmppid)=(xyz813@deweet/prototype1006) already exists.
My configuration is Tomcat 6.0, hibernate 3.3.1, c3pO 0.9.2 or dbcp.
I don’t know how to avoid this I thought that making it UniqueConstraint and calling it in transaction should sole everything.
Could you tip me what am I doing wrong?
@Override
@Transactional(readOnly = false)
public Client createClient(String userid) {
Client c = new Client(userid);
currentSession().save(c);
return c;
}
The client class is defined as below
@Entity
@Table(name = "CLIENT", uniqueConstraints = { @UniqueConstraint(columnNames = { "xmppId" }) })
public class Client {
@Id
private String xmppId;
@Override
public boolean equals(Object o) {
Client c = (Client) o;
if (c.xmppId.equals(this.xmppId))
return true;
return false;
}
@Override
public int hashCode() {
return this.xmppId.hashCode();
}
...
}
It doesnt point directly to this method, but to $Proxy23.createClient but i think it is
the only place I create and save client.
at $Proxy23.createClient(Unknown
Source) at
pl.samsung.cs.deweet.server.RequestHandler.onAddVirtualDevice(RequestHandler.java:182)
at
pl.samsung.cs.deweet.server.RequestHandler.handleRequests(RequestHandler.java:117)
at
pl.samsung.cs.deweet.network.impl.XmppNetContext$8$1.run(XmppNetContext.java:518)
at java.lang.Thread.run(Unknown
Source)
You don’t use auto generated id. That means that you will have to assign it (userId, presumably). When you call
saveit will attempt to insert it every time and you get an exception.Depending on what you’re trying to accomplish, you may use
saveOrUpdatein place ofsave.See this reference http://www.javabeat.net/tips/161-difference-between-hibernates-saveupdate-a.html