-
1. Re: JBoss and PostgreSQL 8.1
waynebaylor Aug 1, 2007 11:32 AM (in response to fermat42)could you please post the code for ComponentPK. also,
you only have 3 fields showing in your posted entity, but the insert has 4. could you also post the rest of the entity code.
just going off of what you have posted i am wondering why you have @IdClass specified when there is only one field annotated with @Id. -
2. Re: JBoss and PostgreSQL 8.1
fermat42 Aug 2, 2007 4:55 AM (in response to fermat42)For sure I can. Sorry I copied only one of the ID Fields:
Server.java:
package de.douglas2a.components;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.ManyToMany;
@Entity
@IdClass(ComponentPK.class)
public class Server implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Id
private Long sessionId;
private String hostname;
private String ip;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public Long getSessionId() {
return sessionId;
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public Server() {
}
public Server(Long sessionId) {
this.sessionId = sessionId;
}
@Override
public int hashCode() {
int hash = 0;
hash += (this.id != null ? this.id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Server)) {
return false;
}
Server other = (Server)object;
if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false;
return true;
}
@Override
public String toString() {
return "de.upb.vdrive.components.Server[id=" + id + "]";
}
}
ComponentPK.java:
package de.douglas2a.components;
public class ComponentPK implements java.io.Serializable {
private Long id;
private Long sessionId;
public ComponentPK() {
}
public ComponentPK(Long id, Long sessionId) {
this.id = id;
this.sessionId = sessionId;
}
public boolean equals(Object obj) {
if (obj == this) return true;
if (!(obj instanceof ComponentPK)) return false;
ComponentPK pk = (ComponentPK)obj;
if (!id.equals(pk.id)) return false;
if (!sessionId.equals(pk.sessionId)) return false;
return true;
}
public int hashCode( ) {
int id = this.id.intValue();
int sid = this.sessionId.intValue();
return id + Integer.reverse(sid);
}
}
If needed I can also post my config-files for configuring Postgre as Datasource... -
3. Re: JBoss and PostgreSQL 8.1
fermat42 Aug 2, 2007 9:24 AM (in response to fermat42)Very Strange... As soon as I remove the line
@IdClass(ComponentPK.class)
and the @Id before the second part of the key everything runs fine. And the same happens with HSQL as DB: I get Exceptions if I use a composed primary key, but there is no error with a normal primary key.
The Problem is: I really need a composed primary key... -
4. Re: JBoss and PostgreSQL 8.1
fermat42 Aug 2, 2007 9:35 AM (in response to fermat42)Hmmm... Much more strange: if I keep my compound primary key but remove the line
@GeneratedValue(strategy = GenerationType.AUTO)
and calculate both parts of the key myself, then everything is o.k.
I do not understand what is going wrong there... Is there a Problem using @GeneratedValue together with a compound primary key? -
5. Re: JBoss and PostgreSQL 8.1
waynebaylor Aug 3, 2007 11:53 AM (in response to fermat42)yes, as far as i know Hibernate will not populate the value when used with a composite PK. To enable that behavior you have to define some custom classes, etc..
We regard this as an extremely strange thing to want to do. If you have a generated surrogate key, why not just make it be the primary key?
However, if you must do this, you can do it by writing a CompositeUserType for the composite identifier class, and then defining a custom IdentifierGenerator that populates the generated value into the composite key class.
http://www.hibernate.org/117.html?cmd=prntdoc
Under the question: "I have a composite key where one column holds a generated value...?"