Solve the problem that the int cannot be blank in Java and the Number in Oracle Database can be

Source: Internet
Author: User
A problem encountered during Hibernate Application Development: a field in the Oracle database (Number type) has no value (that is, NULL), and its JavaBean ing attribute is an int class.

A problem encountered during Hibernate Application Development: a field in the Oracle database (Number type) has no value (that is, NULL), and its JavaBean ing attribute is an int class.

Is NULL the same as zero? Of course not. Null indicates that it does not exist or is not sure. 0 indicates no.
In the development of the application Hibernate, a problem occurs: a field in the Oracle database (Number type) has no value (that is, NULL), and its JavaBean corresponding attribute is of the int type. When obtaining data, the error message "failed to assign NULL value to basic type" is displayed. How can this problem be solved? It is a bit dizzy for a newbie to convert. NET to Java.

1. Let's talk about int and Integer.

Int Is never null because it is a primitive data type. The default value is 0 rather than null.
Integer is an object class, which may be null.

Therefore
Int i1;
Integer i2;

In this case
I1 = 0 is true
If i2 = 0, an error will occur.
I2 = null is true

2. Solution for modifying int to Integer

Change the configuration file:

Make the following changes in JavaBean:

Public void setA252 (Integer a252 ){
If (a252! = Null ){
This. a252 = a252;
}
Else {
This. a252 = 0;
}
}

3. JDBC Solution

String result = rs. getString (1 );

If (result! = Null ){
A252 = Integer. parseInt (result );
}
Else {
A252 = 0;
}

4. Packing, unpacking, and other

Type conversion is only available in 1.5. Before understanding how to make a decision, we must understand what type conversion is. Before 1.5, if you need to convert an int to an Integer, you must do new Integer (intValue); such code work. Therefore, in 1.5, Java creators thought this method was too inefficient, so they introduced the auto-boxing concept in C #. Therefore, you can use the type conversion method to create an Integer class instance. However, the principle is the same as before. In int type, a null Integer object cannot be generated because null values are never allowed.

In addition, you can forcibly convert (Object) int x = null;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.