Hi,
I have a unique constraint on one property of my entity:
Table(uniqueConstraints = @UniqueConstraint(name = "UniqueByEmail", columnNames = "email"))
public class Player implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "email")
private String email;
...
}
In my DAO i want to throw an appropriate exception when something tries to create a new player that would violate this constraint so i need to catch the persistence exception that is thrown when this happens. I wanted to do something like:
public Player create(String email, String password) {
Player player = new Player(email, password);
try {
em.persist(player);
} catch (Exception e) {
System.err.println("I GOT AN EXCEPTION : " + e.getClass());
}
return player;
}
Unfortunately eclipse link doesn't throw the exception like this. when i call flush() on the entity manager i get the persistence exception as a result of this call, but not when i call persist().
I recently started using eclipselink instead of hibernate. since the behavior in this case differs from the behaviour of hibernate, i hope i misconfigured something.
Any help is greatly appreciated.
spring applicationContext.xml:
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistence-unit" />
<property name="jpaDialect" ref="jpaDialect" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter" />
</property>
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="org.eclipse.persistence.platform.database.HSQLPlatform" />
</bean>