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

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

Comments

@spring-projects-issues
Copy link
Collaborator

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"
	class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
	<property name="jpaDialect" ref="jpaDialect" />
	<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
	<property name="dataSource" ref="dataSource" />
</bean>
<bean name="jpaTransactionManager"
	class="org.springframework.orm.jpa.JpaTransactionManager">
	<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

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

	public Long getId() { return id; }
	public void setId(Long id) { this.id = id; }
	public String getName() { return name; }
	public void setName(String name) { this.name = name; }
}
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional("jpaTransactionManager")
@TestExecutionListeners({ TransactionalTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
@ContextConfiguration("classpath:/spring-context.xml")
public class CategoryTest {
	@PersistenceContext
	private EntityManager entityManager;
	
	@Test(expected = DuplicateKeyException.class)
	public void testDuplicateInsert() {
		Category category = new Category();
		category.setId(1L);
		category.setName("Neue Kategorie");
		entityManager.persist(category);
		entityManager.flush();
		
		Category categoryWithSameId = new Category();
		categoryWithSameId .setId(1L);
		categoryWithSameId .setName("Neue Kategorie");
		entityManager.persist(categoryWithSameId );
		entityManager.flush();
	}
}

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
Labels
in: core type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants