初学Hibernate,用的又是最新版本,在运行类似HelloWord程序时出现了Could not parse mapping document from resource...在网上寻找原因,都说出现这种原因一般是配置文件出错,然后我就仔仔细细的查看了配置文件,就是没找出错误,接着我查看了引起异常的地方Duplicate class/entity mapping ,然后我有查看类中的方法,发现我在创建SessionFacotry用错了方法,由于当初创建SessionFacotry时对Hibernate 4 新的创建方法不是很懂,直接复制了网上的方法,导致出现错误,错误代码如下:
Configuration cfg = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());
StandardServiceRegistryImpl registryImpl = (StandardServiceRegistryImpl) builder.build();
SessionFactory sf =
cfg.configure().buildSessionFactory(registryImpl);
解决方案,将上述代码改为SessionFactory sf = cfg.buildSessionFactory(registryImpl);