findByPrimaryKey using Compound Key
mcenita Nov 9, 2004 12:09 AMHi!
Im emplementing a CMP which uses a compound key as the primary key. If I create an entity by calling the create method of the local home interface, the entity was succesfully created. But when i call on the method findByPrimaryKey, it throws an ObjectNotFoundException. This means that it did not found the data i just added. Why it didnt found the data i just added?
Primary Key Class:
public class PR_ContentPK implements Serializable {
public Long getID() {
return ID;
}
public void setID(Long id) {
ID = id;
}
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
public Long ID;
public String lang;
public PR_ContentPK() {
}
public PR_ContentPK(Long ID, String lang) {
this.ID = ID;
this.lang = lang;
}
public int hashCode() {
StringBuffer buffer = new StringBuffer();
buffer.append(this.ID.toString());
buffer.append(this.lang);
String key = buffer.toString();
return key.hashCode();
}
public boolean equals(Object object) {
if (object instanceof PR_ContentPK) {
return ((PR_ContentPK) object).ID.longValue() == this.ID
.longValue() && ((PR_ContentPK) object).lang.equals(this.lang);
} else {
return false;
}
}
public String toString() {
return new String(this.ID + this.lang).toString();
}
}
The method on the session bean
public void addNewArticle(Date date, String title, String content)
throws RemoteException, UnableToAddNewArticleException {
try {
Context ctx = new InitialContext();
PR_HeaderLocalHome headerHome = (PR_HeaderLocalHome) ctx
.lookup(PR_HEADER_JNDI);
PR_HeaderLocal pr_header = headerHome.create(null, date);
PR_ContentLocalHome contentHome = (PR_ContentLocalHome) ctx
.lookup(PR_CONTENT_JNDI);
PR_ContentLocal pr_content = contentHome.create(pr_header.getID(),
"en", title.getBytes(), content.getBytes());
try {
pr_content = contentHome.findByPrimaryKey(new PR_ContentPK(pr_header.getID(),"en"));
} catch (FinderException e2) {
e2.printStackTrace();
}
} catch (NamingException e) {
throw new RemoteException(e.getMessage());
} catch (CreateException e1) {
throw new UnableToAddNewArticleException(
"Unable to create new article.");
}
}
Thanks in advance!