'How does hibernate work with existing tables without adding existing columns(ColumnName to column_name)

When I run my app hibernate creates duplicates of the column and I don't want that to happen. I want it to check if the columns exist and if exist it should ignore them and move. I want to avoid this duplicate and use the first option.

I tried specifying the name of the column I want on the column annotation. and also updatable = false, insertable = false and also



Solution 1:[1]

First you need to get the list of the columns in that class. You can do that by using org.hibernate.metadata.ClassMetadata Entity :

ClassMetadata classMetadata = sessionFactory.getClassMetadata(AppTaskConfig.class);
String[] propertyNames = classMetadata.getPropertyNames();

where propertyNames is an array of Strings representing the property names of AppTaskConfig.

Now using Hibernate org.hibernate.cfg.Configuration object you can find the column names of the properties:

for (String property : propertyNames) {
    Configuration configuration = sessionFactoryBean.getConfiguration();
    PersistentClass persistentClass = configuration
                    .getClassMapping(Details.class.getName());
    String columnName = ((Column) persistentClass.getProperty(property)
                    .getColumnIterator().next()).getName();
}

Also to skip the mapping you can simply use @Transient annotation on your entity.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Heshan Harinda