IllegalStateException: No single default persistent unit defined
From Foochal
When using JPA through spring I got the following error:
java.lang.IllegalStateException: No single default persistence unit defined in {classpath*:META-INF/persistence.xml}
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:230)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:117)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:978)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:462)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:404)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:375)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:263)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:170)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:260)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:184)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:163)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:430)
at
[edit]
org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager
The error was coming from the class org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager, from the following method:
public PersistenceUnitInfo obtainDefaultPersistenceUnitInfo() {
if (this.persistenceUnitInfoNames.isEmpty()) {
throw new IllegalStateException("No persistence units parsed from " +
ObjectUtils.nullSafeToString(this.persistenceXmlLocations));
}
if (this.persistenceUnitInfos.isEmpty()) {
throw new IllegalStateException("All persistence units from " +
ObjectUtils.nullSafeToString(this.persistenceXmlLocations) + " already obtained");
}
if (this.persistenceUnitInfos.size() > 1) {
throw new IllegalStateException("No single default persistence unit defined in " +
ObjectUtils.nullSafeToString(this.persistenceXmlLocations));
}
The last if statement of the above method was failing. It turned out that one of the jars I included already had a META-INF/persistence.xml file.
The map: DefaultPersistenceUnitManager.persistenceUnitInfos contains a physical file location of the jar file which contains the duplicate persistence.xml file.

