0

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.

Dakado
  • 11
  • 2
  • 6

1 Answers1

0

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.

ojsmaina
  • 16
  • 1
  • Is the syntax in columnDefinition DBSYS dependant or is this hybernate syntax ? Can you link some documentation please ? – Dakado Feb 18 '21 at 21:27
  • Yes , its dependent on the underlying db sys you are using hence the indication in the previous comment. "Note: the "int" in the column definition refers to the column data type and hence you need to update it with your requirement" see docs : https://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/ and in this question: https://stackoverflow.com/questions/16078681/what-properties-does-column-columndefinition-make-redundant – ojsmaina Feb 19 '21 at 06:42
  • The hibernate docs indicate "non portable" under the columnDefinition – ojsmaina Feb 19 '21 at 06:53