我对我的实体的一个属性有一个独特的约束:
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;
...
}
在我的 DAO 中,当某些东西试图创建一个违反此约束的新玩家时,我想抛出一个适当的异常:
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;
}
不幸的是,什么也没有发生。当我在实体管理器上调用flush()时,我会因为这个调用而得到持久性异常,但当我调用persist()时却没有。
这种行为与休眠的行为不同,所以我认为我配置错误。
任何帮助是极大的赞赏。
Persistence.xml:org.eclipse.persistence.jpa.PersistenceProvider model.GameInstanceAccount Player
春天应用上下文.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>