Get column name of property mapped with Hibernate

后端 未结 4 1303
眼角桃花
眼角桃花 2020-12-05 18:53

How can I access the Hibernate mapping of my model to find out the column name of a property?

The column name is not specified in the mapping so Hibernate generates

相关标签:
4条回答
  • 2020-12-05 19:26
    ((AbstractEntityPersister) sessionFactory.getClassMetadata(o.getClass()))
        .getPropertyColumnNames(property)[0];
    
    0 讨论(0)
  • 2020-12-05 19:31

    You have to have access to the Hibernate Configuration object.

    0 讨论(0)
  • 2020-12-05 19:47

    This will retrieve one-level composites and normal property mappings:

    String columnName(String name) {
        PersistentClass mapping = configuration.getClassMapping(ExtendedPerson.class.getName());
        Property property = mapping.getProperty(name);
        if(property.isComposite()){
            Component comp = (Component) property.getValue();
            property = comp.getProperty(StringHelper.unroot(name));
            assert ! property.isComposite(); //go only one level down 
        }
        Iterator<?> columnIterator = property.getColumnIterator();
        Column col = (Column) columnIterator.next();
        assert ! columnIterator.hasNext();
        return col.getName();
    }
    
    0 讨论(0)
  • 2020-12-05 19:48

    Thanks to Jherico I found out how to do that:

    ((Column) sessionFactoryBean.getConfiguration().getClassMapping(Person.class.getName())
            .getProperty("myProperty").getColumnIterator().next()).getName();
    
    0 讨论(0)
提交回复
热议问题