insertSelective
allows us to insert only into specified columns, but isn't it the same as insert
, when we keep an object's attributes null which we do not want to insert into the columns?
For example, if a User
class User{
Integer id;
String name;
String address;
}
What is the difference between insert(User{null,"Jack",null}
and insertSelective(User{null,"Jack",null}
?
The difference between
insert()
andinsertSelective()
is thatinsert()
will insert all columns specified in the object, including null values, whileinsertSelective()
will only insert non-null values.So, in your example, if you used
insert(User{null,"Jack",null})
, it would insert a new row with null values for theid
andaddress
columns. If you usedinsertSelective(User{null,"Jack",null})
, it would only insert a new row with the non-null value of "Jack" for thename
column.