[英]I upgraded to Hibernate 6.0 and now I get Unknown Entity errors
Apologies for abusing the format, I'm not really asking a question here but I sure wish this would have shown up when I started banging my head against this wall two weeks ago.为滥用格式而道歉,我并不是真的在这里问一个问题,但我确实希望两周前我开始用头撞墙时会出现这种情况。
I've inherited several large Java projects, using Spring and running on Tomcat, and am upgrading the frameworks used by them.我继承了几个大型 Java 项目,使用 Spring 并在 Tomcat 上运行,并且正在升级它们使用的框架。 However, when I upgrade Hibernate I suddenly get ExceptionInInitializer: root cause UnknownEntityException .
但是,当我升级 Hibernate 时,我突然得到ExceptionInInitializer: root cause UnknownEntityException 。 Our HibernateConf looks like so:
我们的 HibernateConf 看起来像这样:
public class HibernateUtil {
private static final SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
static {
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
}
We're still using hbm.xml to describe mappings, they are placed in individual files and included in hibernate.cfg.xml .我们仍然使用 hbm.xml 来描述映射,它们被放置在单独的文件中并包含在hibernate.cfg.xml中。
Now that I have finally got it to work again, here is what I've learned: Apparently, the boot context is unable to locate hibernate.cfg.xml, but fails silently.现在我终于让它再次工作了,这就是我所学到的: 显然,引导上下文无法找到 hibernate.cfg.xml,但静默失败。 Inside HibernateUtil, I can use addResource and addAnnotatedClass because they use Java's class loader, so I need to list every HBM file and entity class before creating the session factory.
在 HibernateUtil 中,我可以使用 addResource 和 addAnnotatedClass,因为它们使用 Java 的类加载器,所以我需要在创建会话工厂之前列出每个 HBM 文件和实体类。 Like so:
像这样:
public class HibernateUtil {
private static final SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
static {
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
configuration.addResource("path/to/my/mapping.hbm.xml");
configuration.addAnnotatedClass(my.package.for.Persistence.class);
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
}
And now it works.现在它可以工作了。 The reason is that most file lookups are done relative to TomCat's working directory, but a select few use ClassLoader and can therefore more easily find resource files in the source tree.
原因是大多数文件查找是相对于 TomCat 的工作目录完成的,但少数使用 ClassLoader 并且因此可以更容易地在源代码树中找到资源文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.