Solved

Entity Beans using CMP and nullability of database table fields.

Posted on 2001-06-06
5
249 Views
Last Modified: 2013-11-24
If I have an entity bean field modelled as an int, how can the the database be passed a null value for this field using CMP.

Eg. If the employeeID field in the database table allows nulls, and I have the employeeID field modelled as an int in my entity bean, how is it possible to assign the table column a null value?  Obviously, I cannot assign null to an int in Java.

Any ideas?

We're using allaire Jrun 3.0 if that has any implications....
0
Comment
Question by:robinM
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:kylar
ID: 6160849
If you're using primitives, I don't think the container will allow you to put a null into it, despite the databases willingness to accept it. Can you define a number (like -1) as a "unused" or null value, and check for that instead?

Kylar
0
 

Author Comment

by:robinM
ID: 6162873
Yeah, I had kind of thought of that already, but referential integrity requires that to be a valid primary key in another table, which would make the data held non-valid.

I was hoping there was some way round it......
0
 
LVL 4

Accepted Solution

by:
kylar earned 100 total points
ID: 6164729
Well, you could hack around it by not using CMP and when the bean gets passed a '-1' then it records a null value in the table. Something like that?

Kylar
0
 

Author Comment

by:robinM
ID: 6166884
Yeah. But BMP is such a hassle to achieve what should be possible (in my opinion) with CMP.  If nobody comes back with an alternative, I'll award your points. Thanks for your help Kylar
0
 

Author Comment

by:robinM
ID: 6435753
Thanks for your help. The final solution was two fold. For BMP i used the following piece of code.

      con = this.getConnection();
      ps = con.prepareStatement("insert into MBORMUser (cSession, aSession, userTypeKey, ormAccountKey, userName, password, passwordHint, sessionCreationFirstScreen, validateTimeOut, startDate, statusDate, status) " +
                                "values (?,?,?,?,?,?,?,?,?,?,?,?)");
      ps.setInt(1, cSession);
      ps.setInt(2, aSession);
      ps.setInt(3, userTypeKey);
      ps.setNull(4, java.sql.Types.INTEGER);  // on creation, the ormAccountKey should always be null.
      ps.setString(5, userName);
      ps.setString(6, password);
      ps.setString(7, passwordHint);
      ps.setString(8, sessionCreationFirstScreen);
      ps.setInt(9, validateTimeOut);
      ps.setTimestamp(10, startDate);
      ps.setTimestamp(11, statusDate);
      ps.setString(12, status);

Note the ps.setNull() method call, and when using CMP I used the method you suggested that called a stored procedure which amends a value less than 1 to a NULL.....

-- Stored Procedure for creating a new session
-- used in EJB method : createSQL
CREATE PROCEDURE spr_ins_session
@MBORMUserKey int,
@termUser int,
@startDate datetime,
@statusDate datetime,
@status varchar(20),
@lastValidate datetime,
@sessionKey int OUTPUT
AS

BEGIN
     IF @termUser < 1
       SELECT @termUser = NULL

     INSERT INTO session(MBORMUserKey, termUser, startDate, statusDate, status, lastValidate)
     VALUES (@MBORMUserKey, @termUser, @startDate, @statusDate, @status, @lastValidate)

     SELECT @sessionKey = MAX(sessionKey) FROM session

END
GO

Thanks for your help
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

778 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