Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SEAMPERSIST-77 unwrap the exceptions in PersistenceContextProxyHandler #12

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
}