Hibernate升级到5.2 - 创建会话工厂并替换PersistentClass以获取实体类属性

时间:2017-04-25 08:03:37

标签: java hibernate migration sessionfactory

我目前正将Hibernate版本升级到最新版本5.2.10。我在HibernateUtil中替换了我创建SessionFactory的代码。

4.3.11.Final(上一页)

 public class HibernateUtil {
   private HibernateUtil() {}

   private static SessionFactory sessionFactory;

    private static Configuration configuration;

    public static Configuration getConfiguration() {
        return configuration;
    }
    private static SessionFactory buildSessionFactory() {
        try {
                     if(sessionFactory == null) {
                        configuration = new Configuration();
                        configuration.configure("hibernate.cfg.xml");
                        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                                .applySettings(configuration.getProperties()).build();
                        sessionFactory = configuration
                                .buildSessionFactory(serviceRegistry);
                     }
            return sessionFactory;
        }
        catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }

    }
    public static SessionFactory getSessionFactory() {
        return buildSessionFactory();
    }

    public static Session getSession() {
        Session hibernateSession = getSessionFactory().getCurrentSession();
        return hibernateSession;
      }

    public static void shutdown() {
       getSessionFactory().close();
    }
}

5.2.10最终(新):

public class HibernateUtil {
  private static StandardServiceRegistry registry;
  private static SessionFactory sessionFactory;

   public static SessionFactory getSessionFactory() {
        return buildSessionFactory();
   }

  public static SessionFactory buildSessionFactory() {
    if (sessionFactory == null) {
      try {
        registry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
        MetadataSources sources = new MetadataSources(registry);
        Metadata metadata = sources.getMetadataBuilder().build();
        sessionFactory = metadata.getSessionFactoryBuilder().build();
      } catch (Exception e) {
        e.printStackTrace();
        shutdown();
      }
    }
    return sessionFactory;
  }

public static Session getSession() {
        Session hibernateSession = getSessionFactory().getCurrentSession();
        return hibernateSession;
      }

  public static void shutdown() {
    if (registry != null) {
      StandardServiceRegistryBuilder.destroy(registry);
    }
  }
}

现在我有一个方法可以通过将DB表名作为字符串传递来获取列名列表。我之前在4.3.11.Final中做过这样的事:

public static List<String> getColumnNames(String tableName) {

        List<String> columnList=null;

        Map<String, ClassMetadata> map = HibernateUtil.getSessionFactory().getAllClassMetadata();
        Iterator<Entry<String, ClassMetadata>>  itr =  map.entrySet().iterator();

        while(itr.hasNext()){

            ClassMetadata classMetaData = itr.next().getValue();
            AbstractEntityPersister aep = (AbstractEntityPersister) classMetaData;

            if(aep.getTableName().split("\\.")[1].equalsIgnoreCase(tableName)){

                columnList = new ArrayList<String>();
                String[] propertyNames = classMetaData.getPropertyNames();

                for(String property : propertyNames){
                        try {
                            PersistentClass persistentClass = HibernateUtil .getConfiguration().getClassMapping(classMetaData.getEntityName());
                            String clmName =  ((Column) persistentClass.getProperty(property).getColumnIterator().next()).getName();
                            columnList.add(clmName);
                        } catch(NoSuchElementException e){
                            log.error("Element not found idenfied as : "+property);
                        } catch(Exception e){
                            log.error(e.getMessage());
                        }
                }
                break;
            }
        }

        return columnList;
    }

现在升级后,它将方法getAllClassMetadata显示为已弃用,并且我很难获得PersistentClass对象。我看到了类似的问题here,但我无法弄清楚解决方案。我必须更改当前代码的哪一部分,以使我的getColumnNames()方法完全像以前一样工作。我提到了文档,它说要使用EntityManagerFactory.getMetamodel(),但我找不到合适的参考例子。我还要为此更改SessionFactory创建机制吗?

3 个答案:

答案 0 :(得分:2)

最后,我感谢弗拉德的文章。我在没有任何更改的情况下使用了集成商代码,并修改了我的HibernateUtilgetColumns()方法。所以这是我的代码:

创建SessionFactory:

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    public static SessionFactory getSessionFactory() {
        return buildSessionFactory();
    }

    private static SessionFactory buildSessionFactory() {
        final BootstrapServiceRegistry bootstrapServiceRegistry = new BootstrapServiceRegistryBuilder().enableAutoClose()
                .applyIntegrator(MetadataExtractorIntegrator.INSTANCE).build();

        final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder(bootstrapServiceRegistry).configure().build();
        return new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
    }

    public static Session getSession() {
        Session hibernateSession = getSessionFactory().getCurrentSession();
        return hibernateSession;
    }

    public static void shutdown() {
        getSessionFactory().close();
    }
}

元数据提取器(获取列名称):

public static List<String> getColumnNames(String tableName) {

    List<String> columnList = new ArrayList<>();

    for (Namespace namespace : MetadataExtractorIntegrator.INSTANCE.getDatabase().getNamespaces()) {
        for (Table table : namespace.getTables()) {
            if (table.getName().equalsIgnoreCase(lookupTableName)) {
                Iterator<Column> iterator = table.getColumnIterator();
                while (iterator.hasNext()) {
                    columnList.add(iterator.next().getName());
                }
                break;
            }
        }
        if (!columnList.isEmpty())
            break;
    }
    return columnList;
}

答案 1 :(得分:0)

使用hibernate 5获取列名的简便方法

final AbstractEntityPersister classMetadata = (AbstractEntityPersister) sessionFactory.getClassMetadata(clazz)
final String[] names = classMetadata.getPropertyColumnNames(property)

答案 2 :(得分:0)

截至 Hibernate - 5.4.30.Final - 2021 年 3 月 19 日发布

下面的代码有效并且不推荐使用

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)));
        }
 }