Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate add new boolean column to database

I'm trying to add a new bool column into one of the tables using annotations, I've tried

@Column(name = "selected", nullable = true)

private boolean selected;

and then I add the getter/setter but when I run the application I get this error:

Exception occurred inside setter of com.ingens.warranty.model.WarrantyCase.warrantyDetail; nested exception is org.hibernate.PropertyAccessException: Exception occurred inside setter of com.ingens.warranty.model.WarrantyCase.warrantyDetail

I'm pretty new to Hibernate, while my question might be a newbie question but it got me stuck quiet well

Thanks

Edit:

Ok I've found this error in the error stack, Unknown column 'warrantyse14_.selected' in 'field list', apparently the column is not created and the sql select command is using that column which well doesn't exists, so I assume the annotation doesn't work for some reason, it just doesn't create the column after I run the application.

like image 684
arash moeen Avatar asked Mar 21 '13 19:03

arash moeen


1 Answers

Change private boolean selected;

to private Boolean selected;

I think that what happens is that Hibernate tries to set null value from the selected mapped column into the selected field which is primitive hence the exception. It wouldn't have happened if the field had been set to Object instead.

The column definition is useful in conjunction with schema update/validation (hbm2ddl), and the mapping doesn't cover the case where you define a new field by itself . In this case, you'll have to issue two statements:

"alter table ... add column selected ..."

"update ... set selected = false where selected is null"

In order to trigger automatic Hibernate updates to schema (apply for example the @ColumnDefinition), you will have to add the following Hibernate property: hibernate.hbm2ddl.auto=update to persistence.xml provider property if you are using JPA, or as a property in hibernate.cfg.xml if using Hibernate alone

like image 82
Ori Dar Avatar answered Oct 07 '22 16:10

Ori Dar