This works - thank you 

Annette Scherer
Abteilung Informatik


-----Ursprüngliche Nachricht-----
Von: Pinaki Poddar [mailto:ppod...@apache.org] 
Gesendet: Dienstag, 28. Juli 2009 20:56
An: users@openjpa.apache.org
Betreff: Re: update an remove in distributed environments


merge() returns the updated or 'merged' instance.
So please try this and see if that works...
public void remove(Object entity) {
        try {
                EntityManager em = this.getEm();
                Object merged = em.merge(entity);
                em.remove(merged);
        } catch (Throwable t) {
                t.printStackTrace();
        }
} 



Scherer, Annette wrote:
> 
> Hello,
> 
> I am using OpenJPA in a client/server environment.
> I need to persist, find or query an object, update it and remove it.
> 
> How can I do this? As explained in OpenJPA's Manual Part 2, Chapter 8.2, I
> tried to merge the object bevor removing it. But this doesn work. 
> 
> This is my example:
> 
> Client:
> 
> TestObject test = new TestObject();
> test.setNumber(22);
> test.setName("Test22");
> Class testClass = test.getClass();
> 
> try{
>       ... sessionBeanHome.persist(test);
> } catch (Throwable e) {
>       System.out.println(e.getMessage());
> }
> 
> TestObject testRead = null;
> try {
>       testRead = (TestObject)... sessionBeanHome.find(testClass,
> test.getNumber());
> } catch (Throwable e) {
>       System.out.println(e.getMessage());
> }
>               
> try {
>       ... sessionBeanHome.remove(testRead);
> } catch (Throwable e){
>       System.out.println(e.getMessage());
> }
> 
> 
> 
> Server (2.1 SessionBean):
> 
> public void persist(Object entity) {
>       try {
>               this.getEm().persist(entity);
>       } catch (Throwable t) {
>               t.printStackTrace();
>       }
> }
>       
> 
> public Object find(Class entityClass, Object primaryKey) {
>       Object result = null;
>       try {
>               result = this.getEm().find(entityClass, primaryKey);
>       } catch (Throwable t) {
>               t.printStackTrace();
>       }
>       return result;
> }
> 
> public void remove(Object entity) {
>       try {
>               EntityManager em = this.getEm();
>               em.merge(entity);
>               em.remove(entity);
>       } catch (Throwable t) {
>               t.printStackTrace();
>       }
> }     
>               
> 
> My persistence.xml
> 
> <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/persistence_1_0.xsd";
>       version="1.0">
>       <persistence-unit name="Test_DB">
>               <provider>
>                       org.apache.openjpa.persistence.PersistenceProviderImpl
>               </provider>
>               <jta-data-source>java:comp/env/jdbc/testdb</jta-data-source>
>               <class>de.huk.vtp.selektionen.bo.TestObject</class>
>               <properties>
>                       <property name="openjpa.TransactionMode" 
> value="managed"/>
>                       <property name="openjpa.ConnectionFactoryMode" 
> value="managed"/>
>                       <property name="openjpa.jdbc.DBDictionary" value="db2"/>
>                       <property name="openjpa.jdbc.Schema" value="T7"/>
>               </properties>
>       </persistence-unit>
> </persistence>
> 
> 
> 
> Thank you for advice
> 
> Annette Scherer
> Abteilung Informatik
> 
> 
> 


-----
Pinaki 
-- 
View this message in context: 
http://n2.nabble.com/update-an-remove-in-distributed-environments-tp3343960p3345291.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Reply via email to