1

We have a requirement where we need to have the column names externally configurable in Hibernate. The column names in the database will change in the future and we do not want to change the hibernate entity class (annotations) every time this happens.

What is the best approach for this scenario?

saravana_pc
  • 2,607
  • 11
  • 42
  • 66

3 Answers3

1

You can use hibernate mappings files

Check this link maybe it can help you http://www.tutorialspoint.com/hibernate/hibernate_map_mapping.htm

Makis Arvanitis
  • 1,175
  • 1
  • 11
  • 18
1

You cna either use a custom NamingStrategy (see here)

Or define dynamic maping (see here)

Community
  • 1
  • 1
StanislavL
  • 56,971
  • 9
  • 68
  • 98
1

You can keep column names in some class as constants, this way you would only need to update that class when column names change

public class MyEntityColumns {
    public static final String COLUMN1 = "column1";
    ...
}

@Entity
public class MyEntity {
    @Column(name = MyEntityColumns.COLUMN1)
    private String someField;
}
Predrag Maric
  • 23,938
  • 5
  • 52
  • 68