【问题标题】:Where can I get the map of PersistentClass objects that was under org.hibernate.cfg.Configuration in Hibernate 5?我在哪里可以获得 Hibernate 5 中 org.hibernate.cfg.Configuration 下的 PersistentClass 对象的映射?
【发布时间】:2017-04-13 14:20:22
【问题描述】:

在 Hibernate 3 中,在 org.hibernate.cfg.Configuration 类中有一个 Map classes 属性,其中包含 org.hibernate.mapping 类型的对象.PersistentClass.

在一个 Grails 应用程序(它在底层使用 hibernate)中,我正在扩展一个扩展 org.hibernate.cfg.Configuration 的类。这使我可以利用 Map 类并执行以下操作:

for(PersistentClass pc : classes.values()) {
   for(Iterator iterator = pc.getTable().getForeignKeyIterator(); iterator.hasNext(); ) {
       ForeignKey fk = (ForeignKey) iterator.next();               
       iterator.remove();      
 }
}

基本上,我正在删除某些 3rd 方表的外键。

我想升级到 Hibernate 5,但新的 Configuration 类没有我所依赖的 Map 类。

  1. 如何获取 PersistentClass 对象的映射,以便继续删除它们的外键?

  2. 如果不应该再通过配置完成,我如何访问持久对象?通过什么课?

请帮忙。我被卡住了。

【问题讨论】:

标签: hibernate configuration hbm2ddl hibernate-5.x


【解决方案1】:

我想首先,我们应该得到 StandardServiceRegistry 实例,如下所示:

StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure(configFileURL).build();

然后我们可以从这个 StandardServiceRegistry 实例中获取 Metadata 实例,例如:

Metadata metaData = new MetadataSources(standardRegistry).getMetadataBuilder().build();

之后,我们可以从该元数据实例中获取所有 PersistentClasses,如下所示:

Collection<PersistentClass> entityBindings = metadata.getEntityBindings();

这是你需要的吗?

【讨论】:

    猜你喜欢
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 2011-01-11
    • 1970-01-01
    相关资源
    最近更新 更多