0

There is a boolean field in Entity :

@Column(name = "FREEFLAG", columnDefinition = "NUMBER(0,1) default 0", nullable = false)    
public boolean getFreeflag() {
   return freeflag;
}

database - Oracle, field FREEFLAG - NUMBER(0,1)

I try to get object from db with Hibernate, but if the field in db is null i got a exception :

Exception in thread "main" org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type

Why default value doesn't work ? How I can resolve this problem on server side? I have to have 0 or 1 value in db.

I got the solution - Default value not working in hibernate. But I still have to modify database. I have added DDL - alter table client modify freeflag default 0 not null DML - update client set freeflag = 0 where freeflag is null; commit

Community
  • 1
  • 1
idmitriev
  • 4,619
  • 4
  • 28
  • 44

2 Answers2

1

In one of your comments you have said that your database allow null values for this column. The columnDefinition attribute is used by JPA Provider during schema generation process, so I can assume that you have not generated schema after you added columnDefinition attribute (nor nullable=false). Hints in columnDefinition which set default value were not applied to database, so it's nothing strange that you don't have this column defaulted.

Another way you can make it work is creating custom Converter for this field, which will save values in the way you need. More informations about converters implementation available here.

Maciej Dobrowolski
  • 11,561
  • 5
  • 45
  • 67
0

The columnDefinition is database dependent and hence might cause issues sometimes. What I got from searching on net is that it works sometimes but doesnt work in some cases. An alternative is to use the prePersist method technique described in below link:

http://www.coderanch.com/t/450124/ORM/databases/entity-Boolean-field-default

Hope this helps.

Inder
  • 160
  • 9
  • Thx, but this is hack I think. Actually I can add logic to my getter : @Column(name = "FREEFLAG",nullable = false,columnDefinition = "boolean default true") public Boolean getFreeflag() { if (freeFlag == null) return false; return freeflag; } but I dont like this approach. – idmitriev Jul 12 '14 at 14:13