Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Force Insert Query when calling persist()?
Force Insert Query when calling persist()? [message #903285] Wed, 22 August 2012 20:27 Go to next message
Alexander Bätz is currently offline Alexander BätzFriend
Messages: 6
Registered: August 2012
Junior Member

I have a unique constraint on one property of my entity:

Table(uniqueConstraints = @UniqueConstraint(name = "UniqueByEmail", columnNames = "email"))
public class Player implements Serializable {
    @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 {
    } 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 id="entityManager" class="">
    <property name="entityManagerFactory" ref="entityManagerFactory" />

<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" />

<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" />

Re: Force Insert Query when calling persist()? [message #903597 is a reply to message #903285] Fri, 24 August 2012 13:28 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1376
Registered: July 2009
Senior Member
No, flush is defined in the JPA specification to flush out merge/persist operations to the database. EclipseLink delays sending statements to the database until flush or commit is called to allow for batching and other performance optimizations to occur. So if the exception only occurs when the statement is executed, flush should be called

JSR 303 bean validation can also be used to have the exceptions thrown during the persist, without the flush. This might be what you were using in the other environment. EclipseLink support for validation was added in 2.0, and is described here

Best Regards,
Previous Topic:Problem in using SQL Keyword in JPQL
Next Topic:Problem in running QueryHints.READ_ONLY on GF 3.0
Goto Forum:

Current Time: Sun Jan 17 14:04:20 GMT 2021

Powered by FUDForum. Page generated in 0.03171 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top