0

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

1 Answers1

-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.

  • Thank you for answering but this is what I got spring.jpa.hibernate.naming.physicalstrategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl this helps with field naming. And that is what I wanted. – Lonwabo Msingelwa Apr 05 '22 at 07:49
  • I dont think this can be possible. You can refer this and get an idea about that : https://stackoverflow.com/questions/8435558/hibernate-column-only-if-column-exists – Heshan Harinda Apr 05 '22 at 08:13
  • votedown! This answer is copied from an existing post https://stackoverflow.com/a/19421461/6813506 and no refer to the article is given. Flagged as plagiarized answer. – Rohit Gaikwad Oct 13 '22 at 06:00