• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ejbcreate return type

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
     
    He repaced his skull with glass. So you can see his brain. Kinda like this tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic