Unknown entity
org.hibernate.MappingException: Unknown entity: com.xxxx.enity.xxx 按照视频写,老这个问题
org.hibernate.MappingException: Unknown entity: com.xxxx.enity.xxx 按照视频写,老这个问题
2016-04-22
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
//其中 new StandardServiceRegistryBuilder().configure() 这里是默认装载(配置)"hibernate.cfg.xml"这个文件,当然你也可以修改这个文件名称,比如你保存的.cfg.xml文件名为"myHibernate.cfg.xml",那么就在configure()这歌方法里传入"myHibernate.cfg.xml"这个参数就可以了(如下)。
//final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure("myHibernate.cfg.xml").build();
我查阅了hibernate document,在里面扒到了一种可行的办法(以下代码略加修改了一下document里,原始代码看图片):
public static SessionFactory getSessionFactory() throws Exception {
// A SessionFactory is set up once for an application!
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure() // configures settings from hibernate.cfg.xml
.build();
try {
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
}
catch (Exception e) {
// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
// so destroy it manually.
StandardServiceRegistryBuilder.destroy( registry );
}
return sessionFactory;
}
举报