Skip to content

Commit

Permalink
SEAMPERSIST-77 unwrap the exceptions in PersistenceContextProxyHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
maschmid committed Mar 16, 2012
1 parent ddafa63 commit 99b1ca2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
Expand Down Expand Up @@ -53,7 +54,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
return handleCreateQueryWithString(method, args);
}

return method.invoke(delegate, args);
return invokeMethod(method, args);
}

protected Object handleCreateQueryWithString(Method method, Object[] args) throws Throwable {
Expand All @@ -72,7 +73,7 @@ protected Object handleCreateQueryWithString(Method method, Object[] args) throw
}
return query;
} else {
return method.invoke(delegate, args);
return invokeMethod(method, args);
}
}

Expand All @@ -82,4 +83,19 @@ private Expressions getExpressions() {
}
return expressions;
}

/**
* Invokes the method on the delegate and unwraps any original Exceptions
*/
private Object invokeMethod(Method method, Object[] args) throws Throwable {
try {
return method.invoke(delegate, args);
}
catch(InvocationTargetException e) {
if (e.getCause() != null) {
throw e.getCause();
}
throw e;
}
}
}
24 changes: 24 additions & 0 deletions testsuite/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@
<type>pom</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -274,6 +286,18 @@
<type>pom</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
Expand Down Expand Up @@ -66,5 +67,26 @@ public void testManagedPersistenceContext() throws NotSupportedException, System
Assert.assertEquals(1, hotels.size());
transaction.rollback();
}

@Test(expected = EntityNotFoundException.class)
public void testManagedPersistenceContextException() throws NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
transaction.begin();
Hotel h = new Hotel("test3", "Purkynova", "Brno", "CZ", "63100", "Czech Republic");
em.persist(h);
em.flush();
transaction.commit();

transaction.begin();
int affected = em.createQuery("delete from Hotel h where h.name = :name").setParameter("name", h.getName()).executeUpdate();
Assert.assertEquals(1, affected);
transaction.commit();

try {
transaction.begin();
em.refresh(h);
}
finally {
transaction.rollback();
}
}
}

0 comments on commit 99b1ca2

Please sign in to comment.