Environment:
java 1.7.0_25 (64 bit)
jboss 7.1.1.Final
Windows 7
db: db2 9.7
hibernate 4.2.7.SP1
jpa-2.0-api
Hi,
I'm fighting with a combined primary key, which includes an entity reference and a simple value.
Hibernate generates the primary key correctly, but the column size of the column, which references the entity, is 255 instead of 36 (which is the size of the referenced column).
This is what my code looks like:
Note: I use a UUID as primary key within my other entities. The column length for those columns is always 36.
Code:
import static javax.persistence.FetchType.LAZY;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
@MappedSuperclass
@IdClass(AcePK.class)
public abstract class Ace {
protected FilterRule filterRule;
@ManyToOne(cascade = {}, fetch = LAZY, targetEntity = FilterRule.class)
@JoinColumn(name = "RULE_UUID", referencedColumnName="UUID",
updatable = false, nullable = false )
@Id
public FilterRule getFilterRule() {
return this.filterRule;
}
public boolean setFilterRule(FilterRule rule) {
this.filterRule = rule;
}
private String grantedTo;
@Column(name = GRANTED_TO, nullable = false, length = 36)
@Id
public String getGrantedTo() {
return this.grantedTo;
}
public void setGantedTo(String value) {
this.grantedTo = value;
}
}
The class which defines the combined primary key:
Code:
import java.io.Serializable;
import javax.persistence.Column;
public class AcecPK implements Serializable {
private static final long serialVersionUID = -4837448484343854819L;
public String filterRule;
public String grantedTo;
@Column(name = "RULE_UUID", length = 36) //I've tried with and without including this annotation; the result within the db is always the same
public String getFilterRule() {
return filterRule;
}
public void setFilterRule(String rule) {
this.filterRule = rule;
}
public String getGrantedTo() {
return grantedTo;
}
public void setGrantedTo(String granted) {
this.grantedTo = granted;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AcePK acePK = (AcePK) o;
if (filterRule != null ? !filterRule.equals(acePK.filterRule) : acePK.filterRule != null) {
return false;
}
if (grantedTo != null ? !grantedTo.equals(acePK.grantedTo) : acePK.grantedTo != null) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = filterRule != null ? filterRule.hashCode() : 0;
result = 31 * result + (grantedTo != null ? grantedTo.hashCode() : 0);
return result;
}
}
The generated columns within my database look like:
COLUMN_NAME, COLUMN_SIZE, TYPE_NAME, ORDINAL_POSITION
RULE_UUID, 255, VARCHAR, 1
GRANTED_TO, 36, VARCHAR, 2
How can I get rid of this 255 characters?
Thanks for any help.
Camilla