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
[2.1.0] JPA: Parameter value [*] was not matching type [java.util.Map] #851
Conversation
Hey guys, any progress on this one? This is a blocker for upgrading from 2.0.4 for me. |
I just moved to Play from SpringMVC and I encountered exactly the same issue. There is workaround: Instead of passing entities as a parameters, you can pass their identifiers, e.g.: Company company = Company.findById(1l);
List<Computer> data = JPA.em()
.createQuery("from Computer where company.id = :company")
.setParameter("company", company.id)
.getResultList(); Of course this is not ideal and I would like to see this issue fixed. Anybody? |
Still exists in 2.1.1 |
This error happens when specifying the result class of a TypedQuery as well: List<Computer> data = JPA.em()
.createQuery("from Computer", Computer.class)
.getResultList(); This has something to do with the classloaders, as the error results from Hibernate trying to determine the specified result class is assignable from the query result (same when setting the parameter in the query - it makes sure the class is correct). Interestingly (or annoyingly) this does not happen in running play test! The test cases pass, but play run/debug fails. The classloaders/paths will obviously be different between test and run, but I would have thought the models path would be the same in both instances. [This is in 2.1.1] More details -> This is in the else if ( hqlQuery.getReturnTypes().length == 1 ) {
// if we have only a single return expression, its java type should match with the requested type
if ( !resultClass.isAssignableFrom( hqlQuery.getReturnTypes()[0].getReturnedClass() ) ) {
throw new IllegalArgumentException(
"Type specified for TypedQuery [" +
resultClass.getName() +
"] is incompatible with query return type [" +
hqlQuery.getReturnTypes()[0].getReturnedClass() + "]"
);
}
} The private Class determineAssociatedEntityClass() {
try {
return ReflectHelper.classForName( getAssociatedEntityName() );
}
catch ( ClassNotFoundException cnfe ) {
return java.util.Map.class;
}
} |
Has anyone got a workaround for this? |
If you wrap calls to hibernate in: ClassLoader old = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(Play.application().classloader());
// Your hibernate code here
} finally {
Thread.currentThread().setContextClassLoader(old);
} Does this solve it? |
James, import java.lang.reflect.InvocationHandler; public class MyJPA {
} Now, I can use MyJPA.em() instead of play.db.jpa.JPA.em() . I shall switch back from MyJPA.em() to play.db.jpa.JPA.em() when this bug is fixed. |
Hey nchiring, handy work-around! Does anyone know how we get visibility on this bug? I'm worried it isn't even assigned to a milestone yet. Other than having to refactor a complete code base with a work-around above, I would have thought this is blocker for anyone using JPA in anything beyond 2.0.4. Thanks in advance! |
@benlaughlin You have visibility, and I get notified of everything that happens on the issue tracker. |
Thanks jroper! |
I'm also having the same problem that ferdy-lw described above, wherein a classloader-related issue is encountered when specify the result class of a TypedQuery. The issue exists in master (f231601) as well as the java-future branch in richdougherty's fork as of 24cb6e6c. |
@thomas-p-wilson, thanks for checking the java-future branch. Out of interest, does the problem go away if you hack the HttpExecutionContext's execute method to always use the Play ClassLoader? thread.setContextClassLoader(Play.application().classloader()) |
Hey Rich, sorry for the late reply. I'm new to the hacking of the Play Framework internals, so forgive me if this seems a dull question, but is your question a re-iteration of jroper's comment (comment 17145471) from 22 days ago? If so, then I'm pretty sure the answer is yes, but that was Friday. I'll check tomorrow again to make sure. The issue I'm having is that I can't really be wrapping every single data access call with that. I'm totally willing to dive in and try to figure out a solution, but I'm new to hacking the core so I might need some guidance. |
What I mean is that, in my branch (now merged into master), I introduced a new class called So I was wondering if you wouldn't mind editing thread.setContextClassLoader(Play.application().classloader()) This isn't a real fix, but it would provide some helpful information that would lead to a fix. |
Roger that. Thanks for the clarification. I'll do that first thing in the morning. |
The modification did not work as expected. I am still receiving the same issue with TypedQuery that I was before. I'm going to keep messing around with it. At the very least I'll be a little more familiar with the internals after this. As a side-note I'm using Play for Java, as opposed to Scala, lest it make any difference. |
A quick inspection of the context classloader reveals SBT/Play shared ClassLoader, with: WrappedArray(file:[...]), using parent: Common ClassLoader: file:/usr/share/playframework/framework/../repository/cache/com.h2database/h2/jars/h2-1.3.168.jar There are a bunch of jars listed in the WrappedArray, but none are related to my project. |
The fix should have ensured that any code running as a function supplied to an F.Promise would run with the right context ClassLoader set. But it probably won't handle other code. Where was your code running? |
My code is just running in a normal controller. I haven't been able to test On Tue, Jun 4, 2013 at 12:31 AM, Rich Dougherty notifications@github.comwrote:
|
…ntext classloader
The attached pull request fixes this for the 2.1.x branch. As for master, the test should be copied to master, and if it fails, it should be fixed, but I suspect it may already be fixed on master. |
play2-master-PRs #277 FAILURE |
PR validator failed because this PR is against 2.1.x branch, and scalariform hasn't been run against 2.1.x branch yet. |
play2-master-PRs #280 FAILURE |
[2.1.0] JPA: Parameter value [*] was not matching type [java.util.Map]
Nice work guys, thanks very much for fixing this one! |
When fetching a list of models and getting it by a '.setParameter()', instead of getting the list something throws an exception about a not matching type. Here is the code that i used in the sample project:
This is the exception that gets thrown.
@Gissues:{"order":33.333333333333286,"status":"backlog"}