Primary Key in CMP EEJB

Is it necessary to have the hashcode() function and everything if my primary key is just a field? Is there any reason to make my primary key a class if it is only a simple String value? Please let me know.
Thanks,
-Davea

Usually a primary key class is used only if you have a composite primary key .. meaning you have more than one primary key.
hashcode() is needed for the container so that it can manage the primarykey class more efficiently.
When you have a single primary key like String you dont need a primary key class.
Regards,
Meka Toka

Similar Messages

  • Composite Primary Key using  cmp

    How can an Entity Bean with Composite Primary Key using Container Managed Persistance (CMP) be Deployed using in WebLogic Server 8.1
    pls help me

    Your text is a bit confusing, but I think the answer is yes (it can be used).  However, it would help to see the DDL that you are using (or that you propose to use) to avoid any misunderstandings. And please be specific about whether you are referring
    to COLUMNS that are part of a primary key versus the primary key itself (whether it is composed of one column or multiple columns).
    Lastly, why not just try it and see.

  • Primary keys in CMP

    Hello,
    I have 2 CMP having master and detailed relationship.
    1) BookingMasterBean
    2) BookingDetailsBean
    (having one to many relationship).
    This is to do with a new booking to be made for Squash halls in our application.
    The primary key in Master bean , as well as the details bean get generated by a triggers (on insert) in the database(oracle).
    I have a stateful session bean which handles the booking process( creating entities in both master and details). After create a master entity the key should of the master record should be used as one of CMP field value in the details bean.
    i.e., I need to retreive the generated key once ejbCreate has been executed and should be sent back to the Stateful session bean.
    Can anybody tell me how to do this?

    Whatever you have understood is exactly correct. I am passing a value object as a parameter in the ejbCreate method. The code looks as folows.
    CMP header bean:
    public Integer ejbCreate(BookingHeaderValueObject bookingHeader)
    throws CreateException
    setBookingId(new Integer(bookingHeader.getBookingId()));
    setBookingDate(bookingHeader.getBookingDate());
    setBookingSlot(bookingHeader.getBookingSlot());
    setMemberId(bookingHeader.getMemberId());
    setClubId(bookingHeader.getClubId());
    setHallNumber(bookingHeader.getHallNumber());
    setAccompaniedBy(bookingHeader.getAccompaniedBy());
    setAccompaniedType(bookingHeader.getAccompaniedType());
    setTotalCharges(bookingHeader.getTotalCharges());
    setBookingStatus('B');
    setArrivalStatus('N');
    setDateModified(new Date());
    return getBookingId();
    Session Bean code:
    public void bookSquashHall(BookingHeaderValueObject bookingHeader,
    ArrayList bookingDetails )
    throws RemoteException,
    CreateException, NamingException
    int bookingSerialNo = 0;
    BookingHeaderHome bookingHeaderHome = (BookingHeaderHome)
    getEntityHome("BookingHeaderBean");
    BookingDetailsHome bookingDetailsHome = (BookingDetailsHome)
    getEntityHome("BookingDetailsBean");
    BookingHeader bookingHeaderRemote = bookingHeaderHome.create(bookingHeader);
    if( bookingHeaderRemote != null)
    for( int index = 0 ; index < bookingDetails.size(); index++)
    BookingDetailsValueObject bookingDetailsObj = (BookingDetailsValueObject)
    bookingDetails.get(index);
    bookingDetailsObj.setBookingSlNo(bookingSerialNo+1);
    bookingDetailsObj.setBookingId(bookingHeaderRemote.getBookingId().intValue());
    BookingDetails bookingDetailsRemote = bookingDetailsHome.create(
    bookingDetailsObj);
    bookingSerialNo = bookingDetailsRemote.getBookingSlNo().intValue();
    The following are scripts used to create the table structures :
    create table booking_header (
    bh_bookingid number,
    bh_booking_date date not null,
    bh_booking_slot varchar2(11) not null,
    bh__memberid number not null,
    bh__clubid number not null,
    bh_hallno varchar2(7) not null,
    bh_accompanied_by varchar2(60),
    bh_accompanied_type varchar2(10),
    bh_totalcharges number(10,2) not null,
    bh_status char(1) not null,
    bh_arrived char(1) not null,
    bh_date_modified date not null,
    constraint booking_header_pk primary key(bh_bookingid),
    constraint booking_header_fk1 foreign key(bh__clubid) references club_details(cd_clubid),
    constraint booking_header_fk2 foreign key(bh__memberid) references member_details(md_memberid)
    create trigger booking_header_insert
    before insert on BOOKING_HEADER
    for each row
    declare
    newBookingId number;
    begin
    select nvl(max(BH_BOOKINGID)+1, 1) into newBookingId from BOOKING_HEADER;
    :new.BH_BOOKINGID := newBookingId;
    end;
    create table booking_details (
    bd_slno number,
    bd__bookingid number not null,
    bd_booking_date date not null,
    bd_booking_slot varchar2(11) not null,
    bd__memberid number not null,
    bd__clubid number not null,
    bd_hallfacility char(1) not null,
    bd__facilityid number,
    bd_charges number(10,2) not null,
    bd_status char(1) not null,
    bd_arrived char(1) not null,
    bd_date_modified date not null,
    constraint booking_details_pk primary key(bd_slno),
    constraint booking_details_fk1 foreign key(bd__bookingid) references booking_header(bh_bookingid),
    constraint booking_details_fk2 foreign key(bd__clubid) references club_details(cd_clubid),
    constraint booking_details_fk3 foreign key(bd__memberid) references member_details(md_memberid)
    create trigger booking_details_insert
    before insert on BOOKING_DETAILS
    for each row
    declare
    newSlNo number;
    begin
    select nvl(max(BD_SLNO)+1, 1) into newSlNo from BOOKING_DETAILS;
    :new.BD_SLNO := newSlNo;
    end;
    Should I be defining the ejb relationships in this case.?
    Also I saw in a search from google that we can implement ejbPostCreate to retrieve the primary key if generated by the container. Could I do something like that in order to get back the primary key.
    Is there any better approach for this. I am fairly new to the subject .

  • Problem on getting Primary Key in CMP from a Sequence

    hi friends,
    please examin the code,
    // PersonVO.java
    public class PersonVO impelemnts java.io.Serializable{
    // construtctors
    // getter and setter methods
    private int id;
    private String name;
    // PersonEJB.java
    abstract public class PersonEJB implements EntityBean{
    abstract public void setId(int id);
    abstract public void setName(String name);
    abstract public int getId();
    abstract public String getName();
    public int ejbCreate(PersonVO data){
    setId(data.getId());
    setName(data.getName());
    return null;
    // Other callback methods
    // Application logic methods
    Problem :
    the field id of the table is a sequence. how can i get the next value from the table in the ejb.
    i wand to set that value to the
    setid(...) method
    instead of
    setId(data.getId());
    please help......

    Why are you defining a primery key class (PersonVO) for your EJB ?
    If you have not a compound primary key, you dont need define a primary key class like PersonVO, you just have to define an ejbCreate (int id).
    Fil

  • Error While Deploying A CMP Entity Bean With A Composite Primary Key

    Hello all,
    I have a problem deploying CMP Entity beans with composite primary keys. I have a CMP Entity Bean, which contains a composite primary key composed of two local stubs. If you know more about this please respond to my post on the EJB forum (subject: CMP Bean Local Stub as a Field of a Primary Key Class).
    In the mean time, can you please tell me what following error message means and how to resolve it? From what I understand it might be a problem with Sun ONE AS 7, but I would like to make sure it's not me doing something wrong.
    [05/Jan/2005:12:49:03] WARNING ( 1896):      Validation error in bean CustomerSubscription: The type of non-static field customer of the key class
    test.subscription.CustomerSubscriptionCMP_1530383317_JDOState$Oid must be primitive or must implement java.io.Serializable.
         Update the type of the key class field.
         Warning: All primary key columns in primary table CustomerSubscription of the bean corresponding to the generated class test.subscription.CustomerSubscriptionCMP_1530383317_JDOState must be mapped to key fields.
         Map the following primary key columns to key fields: CustomerSubscription.CustomerEmail,CustomerSubscription.SubscriptionType. If you already have fields mapped to these columns, verify that they are key fields.Is it enough that a primary key class be serializable or all fields have to implement Serializable or be a primitive?
    Please let me know if you need more information to answer my question.
    Thanks.
    Nikola

    Hi Nikola,
    There are several problems with your CMP bean.
    1. Fields of a Primary Key Class must be a subset of CMP fields, so yes, they must be either a primitive or a Serializable type.
    2. Sun Application Server does not support Primary Key fields of an arbitrary Serializable type (i.e. those that will be stored
    as BLOB in the database), but only primitives, Java wrappers, String, and Date/Time types.
    Do you try to use stubs instead of relationships or for some other reason?
    If it's the former - look at the CMR fields.
    If it's the latter, I suggest to store these fields as regular CMP fields and use some other value as the PK. If you prefer that
    the CMP container generates the PK values, use the Unknown
    PrimaryKey feature.
    Regards,
    -marina

  • Comp. Primary key class-field mapping to DB columns problem.(EJB 2.0 CMP)

    Hi!
    I have deal with EJB 2.0 CMP bean.
    I have a composite PK so use the java class to store it. And there is a corresponding field in the Entity Bean with its primary key class type.
    The problem is: when deployment tool creates sql query for the findByPrimaryKey(PKClass ck)
    where ck is an object with 3 fielde : a_id, b_id, c_id.
    it says:
    <sql>SELECT "ck" FROM "MyTable" WHERE "ck" = ? </sql>
    And it means by the "ck" simple a column in DB but NOT the composite key which has to be splited into 3 different fields. So the container can't do the proper mapping.
    Does anyone know solutions?

    I was wrong about primary key class fields, take a look at Ejb2.0 Specification (10.8.2):
    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.)
    Right now I'm using BMP and I don't have this problems. Primary keys, calculated fields and types not supported by the DB can be easily achieved. But I understand your interest on CMP, is clearly evolving!

  • Composite Primary Key in M:N CMP Entity Bean Relationship

    Dear Sir/Madam,
         We are creating an Enterprise Application for our institute using EJB 2.0 specifiactions. In the course of developement, we are facing a problem in writing the CMP Entity Beans with EJB Relationships.
         We are having many to many relationship between two beans such that the primary key of one Entity Bean (let's call it A), serves as the foreign key of another Entity Bean (let's call it B). Further, this primary key of A as foreign key in B, participates in the composite key of B.
         The EJB Specifications require that the primary key field(s) of any bean (B, in our case) declared in the Primary Key class should be the subset of the 'cmp-field' declared in the deployment descriptor for that bean. As I said earlier, we have many to many relationship at play. Hence, we require to keep the foerign key in the 'cmr-field' in lieu of 'cmp-field'. In short, the component of primary key is in 'cmr-field' while, it is needed in both 'cmp-field' and 'cmr-field'. That's perfect from Database point of view but illogical from CMP Entity Bean's view.
         How can we write the deployment descriptor for such a CMP (BMP is working fine for above scenario) Entity Bean? Also, how can we write the Primary Key class? The problem is not just to find the solution, but find under the hood of EJB 2.0 specifications.
         Please help.

    I think you should realise that it would not be convenient to use CMP for everything. It is difficult to define complex relations using CMP. Mostly BMP os preferd since it gives the flexibility for the bean developer.
    Regards
    xH4x0r

  • CMP entity bean with compound primary key

    I'm trying to use a compound primary key in a CMP entity bean. I've created the custom primary key class okay, and I have the prim-key-class set in ejb-jar.xml. My client app gets the home reference okay, but when it tries to find an entity, I get the exception "SQLException: Incorrect syntax near '/'."
    I think I'm having trouble deploying this in OC4J. Is there a sample orion-ejb-jar.xml deployment for a custom, compound primary key somewhere?
    Many thanks for any help.
    Ernie

    Never mind. I solved my problem. Thanks.

  • CMP Entity Bean With Primary Key of int

    Is it valid to have a CMP Entity Bean (v1.1) where the primary key is defined as the primitive 'int' instead of a true Java class?
    Thanks in advance,
    Marc

    Hi,
    Ur primary key in the EJB cant be a primitive data type. If the primary key in the table is a int,
    then u will hv [bold] java.lang.Integer [bold] as the datatype in Entity Bean. If u hv further queries write me at [email protected]
    Kesavan.

  • CMP with Auto-increment Primary Key problem

    I'm trying to set a column as IDENTITY in PointBase and have the app server utilize this in the CMP beans. I followed the instructions from the tutorial:
    - In the deployment descriptor, the primary key class is defined as a java.lang.Object. The primary key field is not specified.
    - In the home interface, the argument of the findByPrimaryKey method must be java.lang.Object.
    - In the entity bean class, the return type of the ejbCreate method must be a java.lang.Object.
    Below is (a fragment of) my deployment descriptor:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
    <ejb-jar>
    <display-name>CMPModule</display-name>
    <enterprise-beans>
    <entity>
    <display-name>DocState</display-name>
    <ejb-name>DocState</ejb-name>
    <home>com.sun.itstar.j2ee.projectdocmgt.ejb.DocStateHome</home>
    <remote>com.sun.itstar.j2ee.projectdocmgt.ejb.DocState</remote>
    <local-home>com.sun.itstar.j2ee.projectdocmgt.ejb.LocalDocStateHome</local-home>
    <local>com.sun.itstar.j2ee.projectdocmgt.ejb.LocalDocState</local>
    <ejb-class>com.sun.itstar.j2ee.projectdocmgt.ejb.DocStateBean</ejb-class>
    <persistence-type>Container</persistence-type>
    <prim-key-class>java.lang.Object</prim-key-class>
    <reentrant>False</reentrant>
    <abstract-schema-name>DocState</abstract-schema-name>
    <cmp-field>
    <field-name>name</field-name>
    </cmp-field>
    <cmp-field>
    <field-name>description</field-name>
    </cmp-field>
    <cmp-field>
    <field-name>color</field-name>
    </cmp-field>
    </entity>
    In the admin-serv/logs directory I get the follow message (bad place for the logs, took me a while to find where it was putting it!):
    [24/Sep/2002:16:33:57] WARNING ( 4165): Validation error in bean DocumentType: Warning: All primary key columns in primary table DOCUMENT_TYPE of the bean corresponding to the generated class com.sun.itstar.j2ee.projectdocmgt.ejb.DocumentTypeBean_387898907_JDOState must be mapped to key fields.
    Map the following primary key columns to key fields: DOCUMENT_TYPE.ID. If you already have fields mapped to these columns, verify that they are key fields.

    Great! This worked. Here are some clear steps:
    1. delete the field from the CMP
    2. delete any business methods that you might have for the field
    3. go to the properties of the bean, find the primary key property. Set the property to Unkown Primey Key Class (java.lang.Object)
    Now there is a bigger problem. CMR fields. In ejbPostCreate() the bean gets its own pk and looks up other related beans. Now that I have removed the accessor to the pk field (and the pk field from the bean), how do I lookup the relationships?
    I need some method that I can call from within ejbPostCreate() that will return the primary key...
    I tried adding the field back in but not as the primary key. That generated an exception that said you cannot do that. If the field is the pk it must be marked as the pk.
    Any ideas?

  • CMP with auto-increment primary key.Please help

    Hi all,
    I new with EJB technology so please bear with me.
    Env: 2k Server, SQL 2k Server
    Sun One(Forte EE) to develop and deploy CMP
    I got the servlet to load the data from CMP's business mehods
    is fine. Calling the findPrimaryKey is OK too. Even when I try to insert the row into DB is OK. At this time I make key PK is int AND NOT
    AUTO INCREMENT everything is OK. In the ejbCreate method
    I called setPK, setName ...It's fine.
    The problem comes when I try to make the PK(still int) to be auto-increment field.In ejbCreate not call setPK because of in SunOne IDE, under the J2EE RI tab of EJB Module property, the Auto generate SQL I set to false so that I can
    modify the SQL statement under SQL Deployment.createRow not taking the PK. I thought that will do it but it doesnot like it at all (I got Exception with transaction rollback)I just want to insert a single row with ID and name that's all.
    Then I changed the SQL statment take PK and changed the Auto Generate SQL to True and in ejbCreate the PK as one of the args and called setPK this time with null object. It doesn't like neither.(Exception Cannot set the primary key with null. That is reasonable.)
    I thought using EJB fast and clean technique would help developer to develop application with easiness.
    So I realy stuck with this, I wonder ejb 2.0 support for auto increment at all because I would like to take advantage of the auto increment feature of the DB without writing the PrimaryKeySequence generator at all.
    Am I missing something with this? May be just config thing to tell
    that my PK is auto-increment field so that when the ejbCreate called it knows not taking the PK or even not asking to call setPK in ejbCreate.
    Any help would appreciated.

    Check out this thread, there's a bunch of good info:
    http://www.theserverside.com/patterns/thread.jsp?thread_id=4976

  • Primary key is null in CMP ejbLoad()

    Using WebLogic 7.0 with Oracle 9i Lite.
    We're logging the values of the CMP fields in ejbLoad(). All of the
    CMP values look fine, except for the primary key value; it's always
    null. This means that Foo.getKey() (the CMP field that returns the
    primary key value) always returns null.
    Foo.getPrimaryKey() works as expected.
    This appears to be causing problems with CMR fields. The Collection
    we get from a "many" CMR field is populated with the correct number of
    elements, but if we try to remove one of them we're getting a "primary
    key cannot be null" exception from the container.
    Any insights?
    Richard A. Steele
    Paychex

    Here's some more information:
    We're able to successfully create entity beans; for example, we have beans OrganizationEJB
    and BusinessSiteEJB. There's a one-to-many relationship from OrganizationEJB to
    BusinessSiteEJB.
    Because of database integrity constraints, when we create a BusinessSite, we have
    to provide the Organization as a parameter to create(). This works fine--after calling
    create(), I see the new BusinessSite in the database.
    We can also do a findByPrimaryKey() on BusinessSite and retrieve the business site.
    However, if I do a getBusinessSites() on Organization, I get back a Collection, but:
    1. Sometimes it has just a single entry, even if we know there are multiple business
    sites for this organization. Sometimes, it has the right number of entries, but
    every one of them is identical.
    2. The primary key of the entries in the Collection are all null. In other words,
    if I do a getPrimaryKey() on the BusinessSite local interfaces in the collection,
    I get back null.
    3. If I try to delete any of the Business Sites in the collection, the container
    throws an exception that the primary key can't be null.
    Richard A. Steele
    Paychex

  • How do you change the value of the primary key in the CMP?

    Hi,
    The cmp that I built has the phone as the Prikey field.
    The bean has the get/set methods. The servlet invokes the cmp
    to change the phone number. How could I do that?
    The setXXX for the priKey can not be called outside of the ejbCreate() so
    how would I change the prikey value of a particular row of the table. Do I have
    to remove the bean then create the new bean with the new priKey value?
    Please help,
    Thanks

    Thanks Mona
    Tom.
    Mona Ramlawi <[email protected]> wrote:
    Hi TOM,
    The primary key is the unique identifier of an Entity Bean.
    To change it, you have to remove the bean instance = database delete
    then you have to create a new one with the new primary-key value =
    database insert.
    Hard luck tom, maybe you should consider changing your primary key. You
    can easily change it to an AutoNum
    Tom wrote:
    Hi,
    The cmp that I built has the phone as the Prikey field.
    The bean has the get/set methods. The servlet invokes the cmp
    to change the phone number. How could I do that?
    The setXXX for the priKey can not be called outside of the ejbCreate()so
    how would I change the prikey value of a particular row of the table.Do I have
    to remove the bean then create the new bean with the new priKey value?
    Please help,
    Thanks

  • Creation of CMP bean for a Composite Primary key????

    Hi
    i am having a composite primary keys in one of my table in the database.
    I am trying to create a new entity bean for this table but i don't know how to create one in case when there is a composite primary key for a table.
    Can anybody let me know is it possible to do it.
    what is the procedure to be followed for the creation of the Entity bean in case of a composite primary key.
    I am using MySql as the database .Creating CMP type of Entity bean.
    Any help in this regard will be greatly useful to me as this is very urgent.
    Thanks & Regards
    Vikram K

    Hi Nikola,
    There are several problems with your CMP bean.
    1. Fields of a Primary Key Class must be a subset of CMP fields, so yes, they must be either a primitive or a Serializable type.
    2. Sun Application Server does not support Primary Key fields of an arbitrary Serializable type (i.e. those that will be stored
    as BLOB in the database), but only primitives, Java wrappers, String, and Date/Time types.
    Do you try to use stubs instead of relationships or for some other reason?
    If it's the former - look at the CMR fields.
    If it's the latter, I suggest to store these fields as regular CMP fields and use some other value as the PK. If you prefer that
    the CMP container generates the PK values, use the Unknown
    PrimaryKey feature.
    Regards,
    -marina

  • Setting CMP Primary Key equal to DB primary key

    Is their a mechanism in Oracle JDeveloper(or other tools) for setting the primary key of auto-generated CMP beans to equal the database primary key?
    If so, could someone kindly share their views on the benefits/drawbacks of this approach to CMP?
    -Teodoro Sorgo

    Brice,
    It sounds like you have a page process on every page. Why not just wait to process all of the data on the last page, using your own pl/sql process? Say you have pages 1-3 with items
    P1_ID
    P1_NAME
    P2_DEPT
    P2_LOCATION
    P3_OTHER
    Only have a process on Page 3 have that does
    insert into table (id, name, dept, location, other) values
    (:P1_ID, :P1_NAME, :P2_DEPT ...
    Anton

Maybe you are looking for

  • Java script not working in JSP.

    Hi everyone I've got a problem with a java scipt that's not doing what I want it to do. I have a jsp which gets data from a database to display. On the jsp there is a "add" button which on click will open another jsp to add a contact, when I add the

  • I have download pro cc 2014 , i want to open my resent itmes , how can i do now ??

    I updated the programme to PRO CC 2014 , and i wanna open my resent items, but i press my work , they said "This project was saved in a newer version of Adobe Premiere Pro and cannot be opened in this version " What can i do right now ? thanks so muc

  • Changing my email address account

    Hi all, When I first created my iTunes Store account, I used a gmail address.. I'm willing to change that and replace the gmail address by my me.com address.. I can't do that, when I replace the gmail by the me.com, the system is telling me I can't r

  • How does one print only a highlighted portion of a document?

    How does one print only a highlighted portion of a document?

  • Remove HUM from the System:

    Hi All, The HUM No generated in the system is wrong. The material are packed in the HUM. Now i need to remove the HUM form the System. How is it possible? Avinash