Caused by:org.hibernate.DuplicateMappingException:Duplicate class/entity/ mapping

1、错误描述

java.lang.ExceptionInInitializerError

Caused by:org.hibernate.InvalidMappingException:Could not parse mapping document from resource com/you/model/Monkey.hbm.xml

Caused by:org.hibernate.DuplicateMappingException:Duplicate class/entity/ mapping com.you.model.Monkey


2、错误原因

Configuration config = new Configuration();

config.addClass(Monkey.class);

ServiceRegistry serviceRegistry = new ServiceRegistry().applySettings(config.getProperties().buildServiceRegistry();

sessionFactory = config.buildSessionFactory(serviceRegistry);


3、解决办法

由于配置文件已经添加到Monkey.class,而config.addClass(Monkey.class);又重复添加,故需要将此注释掉

Configuration config = new Configuration();

//config.addClass(Monkey.class);

ServiceRegistry serviceRegistry = new ServiceRegistry().applySettings(config.getProperties().buildServiceRegistry();

sessionFactory = config.buildSessionFactory(serviceRegistry);


你可能感兴趣的:(java,Hibernate,configuration)