How to get Column names of JPA entity

前端 未结 1 727
别跟我提以往
别跟我提以往 2020-12-16 23:34

All my JPA entity classes implement an interface called Entity which is defined like this:

public interface Entity extends Serializable {
// some methods

}
         


        
相关标签:
1条回答
  • 2020-12-17 00:15

    Hibernate can use different naming strategies to map property names, which are defined implicitly (without @Column(name = "...")). To have a 'physical' names you need to dive into Hibernate internals. First, you have to wire an EntityManagerFactory to your service.

    @Autowired
    private EntityManagerFactory entityManagerFactory;
    

    Second, you have to retrieve an AbstractEntityPersister for your class

        SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
        AbstractEntityPersister persister = ((AbstractEntityPersister)sessionFactory.getClassMetadata(baseEntity.getClass()));
    

    Third, you're almost there with your code. You just have to handle both cases - with and without @Column annotation. Try this:

    for (Field field : baseEntity.getClass().getFields()) {
        if (SecureString.class.isAssignableFrom(field.getType())) {
            String columnName;
            if (field.isAnnotationPresent(Column.class)) {
                columnName = field.getAnnotation(Column.class).name();
            } else {
                String[] columnNames = persister.getPropertyColumnNames(field.getName());
                if (columnNames.length > 0) {
                    columnName = columnNames[0];
                }
            }
        }
    }
    

    Note that getPropertyColumnNames() retrieves only 'property' fields, that are not a part of primary key. To retrieve key column names, use getKeyColumnNames().

    And about id field. Do you really need to have all @Id's in child classes? Maybe would better to move @Id to Entity class and mark this class with @MappedSuperclass annotation? Then you can retrieve it just with baseEntity.getId();

    0 讨论(0)
提交回复
热议问题