What happens when we do not define a column in hibernate mapping file (with spring in java) and also do not define a value for that column before doing session.save considering it’s a new object that does not exist in database.
Will the value be null or is there a criteria to decide the value or the behaviour is undefined?
I don’t understand what you mean exactly, but I assume you mean that your table has a column e.g. ext_col
which you have no mapping for in your entity. For example create table test(id integer not null primary key, col1 text, ext_col text)
and an entity like
@Entity
public class Test {
@Id Integer id;
String col1;
}
In that case, Hibernate will not mention the column ext_col
as it obviously doesn’t know about it. This means that the SQL default value will be assigned by your database on insert. So unless you define a default
clause, this will be null
. Update, delete or select statements are not affected by this scenario.
Actually the table has a default value for a column (not mapped with the entity) but there are some entries that have null values in that column. I tried to replicate the issue but failed. Is there a case when this could happen?
Thanks
but there are some entries that have null values in that column
This can only happen when you inserted or updated the column to null. Maybe you forgot to update rows to use the default value instead of null when you introduced the default
for the column? Adding a default will not change existing rows.