好吧,最后我感谢弗拉德的文章。我获取了集成器代码,未做任何更改,并修改了我
HibernateUtil的
getColumns()方法。所以这是我的代码:
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;}


