• Post Reply Bookmark Topic Watch Topic
  • New Topic

2 primary keys??  RSS feed

 
Jolie Lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, how can i declare 2 primary keys in the ejb-jar.xml??



both my packageId and softwareId are primary key, but it seems that i can only have one <primkey-field> defined..

how can i add 2 then?
[ July 07, 2005: Message edited by: Jolie Lee ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having two primary keys in one entity is impossible. What you are looking for is a way to define a composite key. You do that by writing a primary key class, which contains properties for the two fields which make up your composite key. You then specify this class in ejb-jar.xml with the <prim-key-class> element.
 
Jolie Lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've create the primary key class, when i test my page, i got this error:

2005-07-12 09:38:26,142 ERROR [org.jboss.ejb.plugins.LogInterceptor] TransactionRolledbackLocalException in method: public abstract java.util.Collection com.ejb.SoftwarePackageLocal.getPackagesSoftware(), causedBy:
java.lang.InstantiationException: com.ejb.SoftwarePackageItemsKey


this is my primary class:
public class SoftwarePackageItemsKey implements java.io.Serializable {

public Integer packageId;
public Integer softwareId;

public SoftwarePackageItemsKey(Integer packageId, Integer softwareId) {
this.packageId = packageId;
this.softwareId = softwareId;
};

public Integer getPackageId(){
return packageId;
}

public Integer getSoftwareId(){
return softwareId;

}

public void setPackageId(Integer packageId){
this.packageId = packageId;
}

public void setSoftwareId(Integer softwareId){
this.softwareId = softwareId;

}

public boolean equals(Object other) {

if (other instanceof SoftwarePackageItemsKey) {
return (
packageId.equals(((SoftwarePackageItemsKey)other).packageId) &&
softwareId == ((SoftwarePackageItemsKey)other).softwareId);
}
return false;
}

public int hashCode() {

return softwareId.hashCode() + packageId.hashCode();
}
}


and this is my <entity in ejb-jar.xml
<entity>
<description>Software Package Items Entity</description>
<display-name>Software Package Items Entity</display-name>
<ejb-name>SoftwarePackageItemsEJB</ejb-name>
<ejb-class>com.ejb.SoftwarePackageItemsEJB</ejb-class>
<home>com.ejb.SoftwarePackageItemsHome</home>
<remote>com.ejb.SoftwarePackageItems</remote>
<local-home>com.ejb.SoftwarePackageItemsLocalHome</local-home>
<local>com.ejb.SoftwarePackageItemsLocal</local>
<reentrant>False</reentrant>
<persistence-type>Container</persistence-type>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>PackageItems</abstract-schema-name>
<prim-key-class>com.ejb.SoftwarePackageItemsKey</prim-key-class>
<cmp-field><field-name>packageId</field-name></cmp-field>
<cmp-field><field-name>softwareId</field-name></cmp-field>
</entity>


and my relationship in ejb-jar.xml:
<relationships>
<ejb-relation>
<ejb-relation-name>SoftwarePackage-PackageItems</ejb-relation-name>

<ejb-relationship-role>
<ejb-relationship-role-name>package-has-packageitems</ejb-relationship-role-name>

<multiplicity>One</multiplicity>

<relationship-role-source>
<ejb-name>SoftwarePackageEJB</ejb-name>
</relationship-role-source>

<cmr-field>
<cmr-field-name>PackagesSoftware</cmr-field-name>
<cmr-field-type>java.util.Set</cmr-field-type>
</cmr-field>
</ejb-relationship-role>

<ejb-relationship-role>
<ejb-relationship-role-name>packageitems-belongs-to-package</ejb-relationship-role-name>

<multiplicity>Many</multiplicity>
<cascade-delete/>

<relationship-role-source>
<ejb-name>SoftwarePackageItemsEJB</ejb-name>
</relationship-role-source>

</ejb-relationship-role>
</ejb-relation>
</relationships>


anything wrong? pls advise..
 
Jolie Lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got the problem solved. it seems that i missed out an empty constructor in my primary key class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!