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;