-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: persist() Not throwing a "duplicate key" exception
PostPosted: Tue Feb 06, 2007 2:14 am 
Newbie

Joined: Tue Feb 14, 2006 6:53 am
Posts: 7
Hibernate version: Core 3.2.2 EM & A 3.2.1
WebSphere Application Server 6.1, WebSphere JTA, WebSphere DataSources, IBM DB2 V8

Flow: Request to add a Contact with id = '3456' comes through a WebService into a Stateless Session Bean (container transaction = required). EJB delegates to a Orchestrator. (Orchestrator gets an EntityManager and stores it on ThreadLocal). Then the Orchestrator makes an instance of Contact and calls the persist() method on the ContactDAO, passing the Contact instance to the persist() method. The persist() method on the DAO does nothing special in this case, just calls the persist() method on the EntityManager. (When leaving the Orchestrator, the EntityManager gets closed).
This works fine and gets persisted to the DataBase.

When the next request comes in and asks to add a Contact with the same key (3456). The persist() method doesn't throw an exception stating that a Contact entity with that key already exists, but only when the transaction is committed (in the EJB), the driver throws an exception stating that a record with a duplicate key already exists. Surely the persist() method must do this already?

(I understand Hibernate only flushes the DML at certain points, so is the only solution to get the persist() method to throw the exception immediately, to always do a flush() right after calling the persist() method? Waiting for the container to issues the commit and then only getting the duplicate key exception is too late in the process.)

@Entity
@Table(name = "CONTACT")
public class Contact {

private int calculateSomething;

private String firstName;
private String lastName;
private String email;
private String id;

@Column(name = "EMAIL")
public String getEmail() {
return email;
}

@Column(name = "FIRSTNAME")
public String getFirstName() {
return firstName;
}

@Column(name = "LASTNAME")
public String getLastName() {
return lastName;
}

public void setEmail(String string) {
email = string;
}

public void setFirstName(String string) {
firstName = string;
}

public void setLastName(String string) {
lastName = string;
}

@Id
@Column(name = "ID")
public String getId() {
return id;
}

public void setId(String string) {
id = string;
}

@Transient
public int getCalculateSomething() {
return calculateSomething;
}

public void setCalculateSomething(int calculateSomething) {
this.calculateSomething = calculateSomething + 300;
}


}


<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/ ... ce_1_0.xsd" version="1.0">

<persistence-unit name="persistence" transaction-type="JTA">

<jta-data-source>java:comp/env/Hiber DataSource</jta-data-source>

<class>absa.sirius.test.entity.Contact</class>

<properties>
<property name="hibernate.archive.autodetection" value="none"/>

<property name="hibernate.show_sql" value="true"/>

<property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect"/>

<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup"/>

<property name="hibernate.cache.use_second_level_cache" value="false"/>

</properties>

</persistence-unit>

</persistence>


Top
 Profile  
 
 Post subject: Re: persist() Not throwing a "duplicate key" excep
PostPosted: Tue Feb 06, 2007 3:02 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Lindavr wrote:
Surely the persist() method must do this already?

No. If you have to check first, do a query.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.