Solved

Entity Beans using CMP and nullability of database table fields.

Posted on 2001-06-06
5
243 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

759 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