当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Duplicate XML entry | Hibernate orm.xml

问题描述:

Here is an error:

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

And here is my 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>

And here is my AbstractEntity class

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

}

As you can see, there is no duplicates

After debugging, i understand, that file orm.xml was read TWICE!

Here is a screenshot with confirmation of this idea

So how can i tell Hibernate DO NOT read this file twice?

网友答案:

This is an old issue, but might be a current problem as well as was for me...

I was having exactly the same issue and had just figured out that if I remove the reference in persistence.xml to the orm.xml file, it stops happening.

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

Or if I rename it to mapping.xml instead of orm.xml and remap it at persistence.xml it also works.

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

It seems that Hibernate looks for such a file (orm.xml) by default and does not prevent that from being loaded twice in case of persistence.xml is already mentioning it.

Thus, just rename it and remap it, or easier, remove the reference in persistence.xml.

分享给朋友:
您可能感兴趣的文章:
随机阅读: