2016-07-11 42 views
1

這裏是一個錯誤:複製XML條目| Hibernate的orm.xml中

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被讀爲TWICE! 下面是這個想法

enter image description here

所以你怎麼能我告訴Hibernate的不讀這種文件兩次確認截圖?

+0

我猜'AbstractEntity'沒有註釋爲MappedSuperclass對嗎? – Apostolos

+0

你能告訴我們AbstractEntity類嗎? – aksappy

+0

@aksappy我已更新我的主題 – leon0399

回答

3

這是一個老問題,但可能是當前的問題,以及對我來說...

我有完全相同的問題,剛剛想通了,如果我刪除persistence.xml的參考orm.xml文件,它停止發生。

<mapping-file>./META-INF/orm.xml</mapping-file>

或者,如果我把它重命名爲mapping.xml,而不是orm.xmlpersistence.xml它也可以重新映射。

<mapping-file>./META-INF/mapping.xml</mapping-file>

似乎休眠默認查找這樣的文件(orm.xml),並不能防止從在persistence.xml情況下被加載兩次已經提到這一點。

因此,只需重命名它並重新映射它,或者更容易,刪除persistence.xml中的引用。