这是一个错误:
Exception in thread "main" javax.persistence.PersistenceException: Unable to build entity manager factory
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:66)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at ru.leon0399.podius.home.Core.run(Core.java:39)
at ru.leon0399.podius.home.Core.main(Core.java:34)
Caused by: java.lang.IllegalStateException: Duplicate XML entry for ru.leon0399.podius.common.entity.AbstractEntity
at org.hibernate.cfg.annotations.reflection.XMLContext.addClass(XMLContext.java:140)
at org.hibernate.cfg.annotations.reflection.XMLContext.addDocument(XMLContext.java:108)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.<init>(AnnotationMetadataSourceProcessorImpl.java:93)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.<init>(MetadataBuildingProcess.java:147)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:141)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:857)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:884)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
... 4 more
这是我的orm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings
xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
version="2.0">
<mapped-superclass class="ru.leon0399.podius.common.entity.AbstractEntity">
<attributes>
<id name="uuid">
<column name="uuid" column-definition="BINARY(16)"/>
</id>
<basic name="createdAt">
<column name="created_at" updatable="false"/>
<temporal>TIMESTAMP</temporal>
</basic>
<basic name="updatedAt">
<column name="updated_at"/>
<temporal>TIMESTAMP</temporal>
</basic>
</attributes>
</mapped-superclass>
这是我的AbstractEntity类
public abstract class AbstractEntity implements Creatable, Updatable, Entity {
private static final long serialVersionUID = 2544875895016856663L;
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractEntity.class);
private UUID uuid = UUID.randomUUID();
private Date updatedAt;
private Date createdAt;
// Getters and setters
}
如您所见,没有重复
调试后,据我所知,该文件orm.xml
读了两次!
这是一个确认这个想法的屏幕截图
那我怎么能告诉Hibernate不要两次读这个文件呢?
答案 0 :(得分:4)
这是一个老问题,但对我来说可能是当前的问题......
我遇到了完全相同的问题,并且刚刚想出如果我将persistence.xml
中的引用移到orm.xml
文件中,它就会停止发生。
<mapping-file>./META-INF/orm.xml</mapping-file>
或者,如果我将其重命名为mapping.xml
而不是orm.xml
,并将其重新映射到persistence.xml
它也可以。
<mapping-file>./META-INF/mapping.xml</mapping-file>
Hibernate默认情况下会查找这样的文件(orm.xml
),并且在persistence.xml
已提及它的情况下不会阻止加载两次。
因此,只需将其重命名并重新映射,或者更容易删除persistence.xml
中的引用。