This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

need help with jdbc defaulting int to 0  RSS feed

 
slagy maggie
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right now my container managed bean when it does not find a value
it defaults it to "0", but my client does not like it , cause 0 in some cases could make cases could make sense. Can anybody tell me how i can avoid the database from inserting a zero and keeping it null or empty( i know it does this for strings, but not for objects), and i also know it is possible in BMP'S ,by doing the setNull, BUT HOW CAN I DO IT VIA CMP bean
I am using informix as my database.
pllllllllllllllllllleeeeeeeeeeeeeeese help
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Slagy,
Since you are using CMP Entity Beans, you would either need to :

  1. Manipulate the SQL statements for ejbStore() method to configure to your needs.
  2. Assign the value for that variable in the ejbStore() method based on your business requirements.

  3. As regards the first case, a proper SQL command for that variable is required.For example, in Oracle8i to avoid null values I use NVL command.Look out for similar command for your int column in Informix DB, to default it to the value you want.
    Hope this helps.
    Regards,
    Sandeep

    • Sun Certified Programmer for Java 2 Platform Scored 93 per cent
    • Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
    • IBM Enterprise Connectivity with J2EE Scored 72 per cent
    • Enterprise Development on the Oracle Internet Platform Scored 44 out of 56

    • [This message has been edited by Desai Sandeep (edited May 12, 2001).]
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A primitive int simply has no representation for the null value. Have you tried using a java.lang.Integer field? As a last resort, you can always go to BMP (in a subclass of the CMP bean, if you want to keep all your options open).
- Peter
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter,
Could you please explain what you meant by this:

you can always go to BMP (in a subclass of the CMP bean, if you want to keep all your options open).

I didn't get the subclass thing???
Thanks in advance.
Sandeep
[This message has been edited by Desai Sandeep (edited May 13, 2001).]
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, it was a bit opaque, wasn't it? What I meant is that you can take a CMP bean, create a subclass of it, and implement all the persistence code in the subclass. In fact, EJBs were designed with that option in mind: it's one of the ways in which an EJB container may implement CMP.
This way, you can decide between CMP and BMP when deploying the bean, and let the choice depend on how powerful and efficient the container's CMP features are. If you would redeploy on a platform which handles nulls the way you want it, you can just ignore your BMP subclass. Implementing persistence in a subclass can give you a clean separation between bean logic and database code as well.
- Peter
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
Yes, now I got it!!It is one of the ways by which you could take the advantage of your persistence logic along with the Container Managed Services of the EJB.
I remember having read about this long time back, when I started with EJB (probably in Ed Roman, not sure!!), but never had a chance to implement it in this fashion.Does it help to subclass a CMP Bean as a BMP ,and deploy both of them together?
Yes the one thing which you have mentioned is about the way you want to the null values in your BMP subclass.Any other advantage?
Thanks,
Sandeep
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Desai Sandeep:
Does it help to subclass a CMP Bean as a BMP ,and deploy both of them together?

Not entirely sure what you mean here -- you would deploy either the CMP EJB class or its BMP subclass, not both. I last used this when developing EJBs for a not-too-flexible application server, where the customer had an eye on migrating to a more powerful platform in the future. The beans could possibly be deployed as CMP.
Yes the one thing which you have mentioned is about the way you want to the null values in your BMP subclass.Any other advantage?

What I would do is simply use a java.lang.Integer as the persistence field, use code like

to store the value, and

to retrieve it. This makes sure that a null really remains a null. I'd still be surprised if CMP wouldn't do this though.
- Peter
PS. Obviously, in the last code snippet you could save some garbage collection by storing the value in an intermediate primitive int and creating the Integer only when necessary, but I'm not sure whether the savings will be worth the additional code clutter.
[This message has been edited by Peter den Haan (edited May 15, 2001).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!