Skip to main content

@IdClass help

2 replies [Last post]
dibyendumajumdar
Offline
Joined: 2005-01-15

Hi,

I am trying to use @IdClass annotation to define a composite primary key. I have tried various combinations but everytime I get following error:

Caused by: Exception [TOPLINK-7149] (Oracle TopLink Essentials - 10g release 4 (10.1.4.0.0) (Build 051215Dev)): oracle.toplink.essentials.exceptions.ValidationException
Exception Description: The names of the primary key fields or properties in the primary key class [class entity.tpcc.WarehousePK] and those of the entity bean class [class entity.tpcc.Warehouse] must correspond and their types must be the same.
at oracle.toplink.essentials.exceptions.ValidationException.invalidCompositePKSpecification(ValidationException.java:856)
at oracle.toplink.essentials.internal.annotations.EJBAnnotationsProcessor.processEntityClass(EJBAnnotationsProcessor.java:1190)
at oracle.toplink.essentials.internal.annotations.EJBAnnotationsProcessor.processORAnnotations(EJBAnnotationsProcessor.java:1956)
at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:314)
at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer.callPredeploy(JavaSECMPInitializer.java:140)
at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer.initPersistenceUnits(JavaSECMPInitializer.java:279)
at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer.initialize(JavaSECMPInitializer.java:298)
at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer.initializeFromAgent(JavaSECMPInitializer.java:315)

Here are the two relevant classes:

@Entity(access = AccessType.FIELD)
@Table(name = "WAREHOUSE", schema = "TPCC")
@IdClass(entity.tpcc.WarehousePK.class)
public class Warehouse {

@Id
@Column(name = "W_NAME", length = 10)
String name;

@Id
@Column(name = "W_CITY", length = 20)
String city;

@Version
@Column(name = "W_VERSION")
int version = 1;

@Column(name = "W_STREET_1", length = 20)
String street1;

@Column(name = "W_STREET_2", length = 20)
String street2;

@Column(name = "W_STATE", length = 2)
String state;

@Column(name = "W_ZIP", length = 9)
String zip;

@Column(name = "W_TAX", precision = 4, scale = 0)
BigDecimal tax = new BigDecimal(0);

@Column(name = "W_YTD", precision = 12, scale = 0)
BigDecimal ytd = new BigDecimal(0);

public final String getName() {
return name;
}

public final String getCity() {
return city;
}

public final String getState() {
return state;
}

public final String getStreet1() {
return street1;
}

public final String getStreet2() {
return street2;
}

public final BigDecimal getTax() {
return tax;
}

public final int getVersion() {
return version;
}

public final BigDecimal getYtd() {
return ytd;
}

public final String getZip() {
return zip;
}

public final void setCity(String city) {
this.city = city;
}

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

public final void setState(String state) {
this.state = state;
}

public final void setStreet1(String street1) {
this.street1 = street1;
}

public final void setStreet2(String street2) {
this.street2 = street2;
}

public final void setTax(BigDecimal tax) {
this.tax = tax;
}

public final void setVersion(int version) {
this.version = version;
}

public final void setYtd(BigDecimal ytd) {
this.ytd = ytd;
}

public final void setZip(String zip) {
this.zip = zip;
}

@Override
public String toString() {
return "name=" + getName();
}

}

public class WarehousePK implements Serializable {

private static final long serialVersionUID = 1L;
public String name = "";
public String city = "";

public WarehousePK() {
}

public final String getCity() {
return city;
}

public final String getName() {
return name;
}

public final void setCity(String city) {
this.city = city;
}

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

@Override
public boolean equals(Object arg0) {
if (arg0 == this) {
return true;
}
else if (arg0 instanceof WarehousePK) {
return name.equals(((WarehousePK)arg0).name) && city.equals(((WarehousePK)arg0).city);
}
return false;
}

@Override
public int hashCode() {
return name.hashCode() ^ city.hashCode();
}
}

I have tried removing the @Column annotations, but it doesn't help.

The documentation on @IdClass is very vague and there aren't any real examples on how to use this annotation.

Regards

Dibyendu

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
guypelletier
Offline
Joined: 2005-10-19

Hi Dibyendu,

The currently implementation does not allow for anything besides id members to be defined in the IdClass. This was based on a previous version of the EJB 3.0 spec.

So, if you remove the:
private static final long serialVersionUID = 1L;
you should not see this error. This limitation will be fixed in a future build.

Cheers,
Guy

dibyendumajumdar
Offline
Joined: 2005-01-15

Thanks - removing serialVersionUID does indeed solve the problem.

I do think that the EJB spec is not clear on @IdClass and I have summarised the problems at http://trycatchfinally.blogspot.com/2006/01/problems-with-ejb-30-idclass... and also reported same to the EJB feedback address.

Regards

Dibyendu