Issue
I use Hibernate with MariaDB, when I am updating my application I sometimes need to add property to existing entities, but when I do that, hibernate indeed adds that new column to the MariaDB table, however all values are NULL in that column and hibernate refuses to start and throws ton of errors:
Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'metaDataSourceAdvisor': Cannot resolve reference to bean 'methodSecurityMetadataSource' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available
To solve this, I have to login to the database manually and set values for the new column created, then hibernate starts again with no errors. But I am seeking for a way to define default for that column so I dont have to do that and that process is automatic.
Solution
You can initialize / set a default value when you create the field as follows
@Column(name = "myColumn", nullable = false, columnDefinition = "int default 100")
Note: the "int" in the column definition refers to the column data type and hence you need to update it with your requirement.
Answered By - ojsmaina
Answer Checked By - Clifford M. (JavaFixing Volunteer)