Hibernate adds new boolean column to database

I am trying to add a new bool column to one of the tables using annotations, I tried

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

private boolean selected;

and then I add 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 new to Hibernate, while my question might be a newbies question, but it got stuck calmly.

thanks

Edit:

Good. I found this error in the error stack, Unknown column 'warrantyse14_.selected' in 'field list' , apparently the column was not created, and the select sql command uses this column, which does not exist, so I assume that the annotation does not work for any For that reason, it just does not create a column after starting the application.

+4
source share
1 answer

Edit private boolean selected;

in private boolean selected;

I think what happens is that Hibernate is trying to set a value of zero from the selected mapped column to the selected field, which is primitive, therefore an exception. This would not happen if the field were set to Object instead.

Defining a column is useful in combination with updating / checking the schema (hbm2ddl), and the mapping does not apply to the case when you define a new field yourself. In this case, you will need to issue two statements:

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

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

To initiate an automatic Hibernate update for a schema (e.g. @ColumnDefinition ), you will need to add the following Hibernate property: hibernate.hbm2ddl.auto=update to the persistence.xml provider property if you use JPA or as a property in the hibernate.cfg.xml file when using only hibernate

+5
source

All Articles