CMP 2.0 Using primitive type for fields that have null values

Posted on 2005-04-26
Last Modified: 2010-03-31
My problem is I have a CMP 2.0 Bean and one of columns is of type int and I'm using int as the return type in getAccessAuthorizationId() instead of java.lang.Integer.  When I query a bean by Id and it gets to that column I get a java.lang.NullPointerException.

My question:  Is there an xdoclet tag that I can set to allow the query of a null value on a primitive and just let the container set the value to the primitives default.  I tried the following tag

     * Getter for CMP Field companyName
     * @ejb.persistent-field
     * @ejb.interface-method   view-type="both"
     * @jboss.column-name name="access_authorization_id"
     * @jboss.persistence not-null="true" ///THIS didn't work, and since by default its false and that doesn't work either I don't think this is working.
    public abstract int getAccessAuthorizationId();
Question by:phoenixsilver
    LVL 15

    Expert Comment

    I don't think so, what value do you expect when its Null ? 0, -1, ...
    You will need to change it to Integer.
    public abstract Integer getAccessAuthorizationId();

    Author Comment

    I wanted a zero, or at least some way of specifying a default value, cause I don't want to have to check for a null for all option column names.  I know the current way makes sense regarding entities as a null column should be null.  But I think you know hwat I mean, I guess I'll check in my object factories where I take entities and turn them into business objects for nulls.  
    LVL 15

    Accepted Solution

    >> But I think you know hwat I mean
    Yes, I know what you mean but I think this is a dangarous aproach (to have zero in your business layer but null in DB, what will happen when they will set it back...)
    If zero is a value that should indicate "empty" then don't set your DB colum to NOT NULL. (or at least don't tell that to the CMP layer [if you are read-only CMP]).

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now