1

I'm generating the postgresql table schema using hibernate:

@Entity
public class MyEntity {
    @Id
    private long id;

    private String name;
    private int age;

    @Column(name = "testdate")
    private Date thedate;
}

As a result I get:

CREATE TABLE ...

The table fields are all created in alphabetic order, no matter how the field order in the @Entity is.

When I add additional fields lateron, they are usually just appended as an extra column in the database, and not inserted in alphabetic order in between.

Question: how can I read all generated table names, in their order of insertion, from the database? And with their generated schema column names? Is that possible at all?

Sidenote: I'm maintaining the data through springs CrudRepository only.

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • You can get a `ClassMetadata` object from the `SessionFactory`: http://stackoverflow.com/questions/634342/get-the-table-name-from-the-model-in-hibernate. Is this what you need? – René Link Jul 05 '16 at 10:42

3 Answers3

2

If you have the org.hibernate.cfg.Configuration you can access the PersistentClasses and the Tablees.

public void printTables(Configuration configuration) throws MappingException {  
  Iterator classMappingIterator =   configuration.getClassMappings();

  while(classMappingIterator.hasNext()) {
    PersistentClass persistentClass = (PersistentClass)classMappingIterator.next();

    Table table = persistentClass.getTable();
    String tableName = table.getName();

    Iterator columnIterator = table.getColumnIterator();

    while(columnIterator.hasNext()){
        Column column = (Column) columnIterator.next();
        String columnName = column.getName();

        System.out.println(tableName + "." + columnName);
    }

  }
}

In spring you can get the Configuration through the LocalSessionFactoryBean.

 ApplicationContext appContext = ...;

 LocalSessionFactoryBean lsfb = (LocalSessionFactoryBean) appContext.getBean("&sessionFactory");

You must use the & prefix to get the factory bean. Otherwise you will get the bean that the factory creates.

You can get the ApplicationContext in any other spring bean by implementing ApplicationContextAware.

René Link
  • 48,224
  • 13
  • 108
  • 140
2

As of Hibernate - 5.4.30.Final - released March 19th, 2021

the method getAllClassMetadata() is no longer supported since few years ago
The code below works and is not deprecated :

MetamodelImplementor metaModelImpl = (MetamodelImplementor)session.getMetamodel();
Map<String, EntityPersister> entityPersisters = metaModelImpl.entityPersisters();
Collection<EntityPersister> val = entityPersisters.values();               

for (EntityPersister ep : val) {
        AbstractEntityPersister aep = (AbstractEntityPersister)ep;

        System.out.println(aep.getTableName());
        System.out.println(Arrays.toString(aep.getIdentifierColumnNames()));
        for (String propName : aep.getPropertyNames()) {
               System.out.println(propName);
               System.out.println(Arrays.toString(aep.getPropertyColumnNames(propName)));
        }
 }
M. Amer
  • 916
  • 10
  • 12
0

using

mSession.getSessionFactory().getAllClassMetadata();

returns a Map wich you can work with.

vLopez
  • 445
  • 7
  • 15