File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes ejbcreate return type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "ejbcreate return type" Watch "ejbcreate return type" New topic
Author

ejbcreate return type

Raj Puri
Ranch Hand

Joined: Apr 24, 2003
Posts: 189
In EJB 2.0(CMP) can the return type needs to be String, Integer etc or can this be an object class like custPK(assumming we have this defined as below). To me seems this will be illegal as return type needs to be declared in DD.
The reason for this question is that I have some old code (EJB 1.1)that runs into problem when running in OC4J new version(EJB 2.0) that is suppoSed to be backwarD compAtible and I am thinking it is not.

Please confirm.
-------------------------------------

import java.io.Serializable;

public class CustPK implements Serializable
{
public String sEventId;

public CustPK()
{
}

public CustPK(String _eventid)
{
this.sEventId = _eventid;
}

public int hashCode()
{
// Add custom hashCode() impl here
return super.hashCode();
}

public boolean equals(Object other)
{
// Add custom equals() impl here
return super.equals(other);
}
}
[ October 06, 2005: Message edited by: Raj Puri ]
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2921
    
    5
Originally posted by Raj Puri:
In EJB 2.0(CMP) can the return type ... be an object class like custPK(assumming we have this defined as below).

You can return an Object as a primary key - your case is a bit strange as your primary key object has only one field. In that case the spec would prefer you simply stick to that field in the bean itself(14.1.9).

Lets pretend this works (it might) - you will still have to include the virtual field
public abstract String getSEventId();
public abstract void setSEventId(String sEventId);
and the associated abstract schema definition on your CMT entity bean (14.1.9.2).
However you would not specify <primkey-field> (this is standard practice for a compound primary key).

14.1.9.2 Primary key that maps to multiple fields in the entity bean class
The primary key class must be public, and must have a public constructor with no parameters. All fields in the primary key class must be declared as public. The names of the fields in the primary key class must be a subset of the names of the container-managed fields. (This allows the container to extract the primary key fields from an instance�s container-managed fields, and vice versa.)


Originally posted by Raj Puri:
To me seems this will be illegal as return type needs to be declared in DD.

I don't see a problem there - you just give the fully qualified name of your primary key class.
<prim-key-class>FullyQualifiedPackage.CustPK</prim-key-class>
Raj Puri
Ranch Hand

Joined: Apr 24, 2003
Posts: 189
I am trying to use oracle sequence generator to generate next sequence that is used by the table as primary key here(custpk). The way to do it in oracle is that you send an insert omitting the PK column and backend insert trigger will generate the next sequence and Oracle will take care of PK. So kind of EJB is only responsible for all columns except PK. So code returns a null key.

This used to work fine with EJB 1.1 and now I get error like can not call
getprimarykey() inside Ejbcreate see chapter 10.5.4 of EJB 2.0 specs. Really all Ejbcreate is doing is setting all columns and returning null as PK per specs.

Is there any clue on this? I have spent a number of hours reading on this.
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2921
    
    5
Dealing with that type of primary key generation is very much application server dependent. JBoss has an <unknown-pk> section. Oracle's OJ4 specifies this in its FAQ:

OC4J-FAQ-EJB-1012

37. How can I use a system generated primary key?
If you specify java.lang.Object as the primary key class type in <prim-key-class>, but do not specify the primary key name in <primkey-field>, then the primary key is auto-generated by the container. OC4J names the corresponding database column as autoid by default.


38. Can I use a database sequence for a primary key?
OC4J currently does not support a database sequence for primary key for CMP entity beans. You have to use a stateless session bean or a Java class to use a sequence to generate primary key for your CMP entity bean. The FAQ demo application shipped with OC4J has an example of PK Generator using a database sequence.


If you continue to use the trigger in the established fashion you are going to need an ejbLoad before your primary key is loaded. That is too late as the container needs the primary key to create the EJBObject before it calls ejbPostCreate.

The avenues I would pursue:
  • Modify the trigger to test whether the INSERT contains a primary key - and only provide one if it does not.
  • In the ejbCreate access the sequence yourself to obtain the primary key. You may be able to do it in the ejbCreate or in a helper method of the bean - you may have to access the sequence through a stateless session bean, possibly through a stored procedure.


  • [ October 09, 2005: Message edited by: Peer Reynders ]
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: ejbcreate return type
     
    Similar Threads
    How to compare the new student with ArrayList.
    Question 51 in John Meyer's mock Exam
    equals hashcode
    Equals( ) method semantics
    Please Explain (Hashcode)