Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ORM PersistenceExceptionTranslation does not convert Hibernate3 unique ids error to spring DuplicateKeyException [SPR-8996] #13636

spring-projects-issues opened this issue Jan 5, 2012 · 0 comments
in: core type: enhancement


Copy link

spring-projects-issues commented Jan 5, 2012

Christoph Jerolimov opened SPR-8996 and commented

When using spring-orm with Hibernate it is possible to convert the Hibernate Exceptions to the Spring DataAccessException by simple adding the PersistenceExceptionTranslationPostProcessor.

But this does not work for the Hibernate Exception org.hibernate.NonUniqueObjectException which will converted in org.springframework.orm.hibernate3.SessionFactoryUtils#convertHibernateAccessException in the last line (as fallback) to an org.springframework.orm.hibernate3.HibernateSystemException.

I think the correct exception will be org.springframework.dao.DuplicateKeyException (prefered) or the parent org.springframework.dao.DataIntegrityViolationException. Implemented just simuluar to the other mappings in SessionFactoryUtils#convertHibernateAccessException.

The Hibernate NonUniqueObjectException exist since Hibernate 3.0. Problem verified with Spring-ORM 3.0 and 3.1.

<bean id="jpaDialect"
	class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
<bean id="jpaVendorAdapter"
	class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
<bean id="entityManagerFactory"
	<property name="jpaDialect" ref="jpaDialect" />
	<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
	<property name="dataSource" ref="dataSource" />
<bean name="jpaTransactionManager"
	<property name="entityManagerFactory" ref="entityManagerFactory" />

<!-- Optional exception translation -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
public class Category implements Cloneable {
	// No ID Generation here..
	private Long id;
	private String name;

	public Long getId() { return id; }
	public void setId(Long id) { = id; }
	public String getName() { return name; }
	public void setName(String name) { = name; }
@TestExecutionListeners({ TransactionalTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
public class CategoryTest {
	private EntityManager entityManager;
	@Test(expected = DuplicateKeyException.class)
	public void testDuplicateInsert() {
		Category category = new Category();
		category.setName("Neue Kategorie");
		Category categoryWithSameId = new Category();
		categoryWithSameId .setId(1L);
		categoryWithSameId .setName("Neue Kategorie");
		entityManager.persist(categoryWithSameId );

Affects: 3.0.5, 3.1 GA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
in: core type: enhancement
None yet

No branches or pull requests

2 participants