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 16:27 Go to next message
Alexander Bätz is currently offline Alexander Bätz
Messages: 6
Registered: August 2012
Junior Member
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>

Re: Force Insert Query when calling persist()? [message #903597 is a reply to message #903285] Fri, 24 August 2012 09:28 Go to previous message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1017
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 http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/validation_api_integration

Best Regards,
Chris
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: Thu Aug 28 21:39:51 EDT 2014

Powered by FUDForum. Page generated in 0.01612 seconds