Problem with Query Creation
Added by Jeffrey Cutler 5 months ago
I have an entity that had been working and for some reason it no longer compiles. The entity has a named query as shown below that is not being retrieved by Hades during runtime weaving. I have other classes that are compiling properly, but I've been unable to figure out why this one used to work but no longer does. Any suggestions on how to troubleshoot this?
Thanks!
Jeff
I'm using Hades 1.0.0 with OpenJPA on Tomcat.
persistence.xml
1 <class>net.jpcutler.model.Sponsor</class>
Sponsor.java
1 @NamedQuery(name = "Sponsor.findByNameInsensitive", query = "SELECT s FROM Sponsor s WHERE s.status.statusCde = 1 AND UPPER(s.sponsorName) LIKE :sponsorName"),
SponsorDAO.java
1 public List<Sponsor> findByNameInsensitive(String name);
2 public List<Sponsor> findByNameInsensitive(String name, Pageable page);
Caused by: org.synyx.hades.dao.query.QueryCreationException: Could not create query for method public abstract java.util.List net.jpcutler.persist.SponsorDAO.findByNameInsensitive(java.lang.String)! Could not find property NameInsensitive on domain class Sponsor.
at org.synyx.hades.dao.query.QueryCreationException.invalidProperty(QueryCreationException.java:38)
at org.synyx.hades.dao.query.QueryCreator.buildAndPart(QueryCreator.java:112)
at org.synyx.hades.dao.query.QueryCreator.constructQuery(QueryCreator.java:71)
at org.synyx.hades.dao.query.SimpleHadesQuery.<init>(SimpleHadesQuery.java:60)
at org.synyx.hades.dao.query.SimpleHadesQuery.construct(SimpleHadesQuery.java:138)
at org.synyx.hades.dao.query.QueryLookupStrategy$3.resolveQuery(QueryLookupStrategy.java:80)
at org.synyx.hades.dao.query.QueryMethod.<init>(QueryMethod.java:104)
at org.synyx.hades.dao.orm.GenericDaoFactory$QueryExecuterMethodInterceptor.<init>(GenericDaoFactory.java:358)
at org.synyx.hades.dao.orm.GenericDaoFactory.getDao(GenericDaoFactory.java:175)
at org.synyx.hades.dao.orm.GenericDaoFactoryBean.getObject(GenericDaoFactoryBean.java:95)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport$1.run(FactoryBeanRegistrySupport.java:121)
Replies
RE: Problem with Query Creation
-
Added by Oliver Gierke 5 months ago
Hades does not seem to find the named query, tries to create it itself and failes as it checks the generated query paramteres to be present in the entity class. What do you mean with "no longer" under which circumstances did it work?
You should consider upgrade to 1.5 (should be a drop in replacement) because 1.0 contained a serious bug regarding the use of Persistable in query methods.
Regards,
Ollie
RE: Problem with Query Creation
-
Added by Jeffrey Cutler 5 months ago
I updated a coworker's code that did not touch any of these 3 files. Since then it doesn't work on my machine. I haven't been able to find anything else that has changed in the setup of the system... hence why I'm perplexed.
Which class and method checks to see if the named query exists? Perhaps I could add some logging to it to try and figure out what is going on.
I'll try dropping 1.5 now.
Thanks,
Jeff
RE: Problem with Query Creation
-
Added by Jeffrey Cutler 5 months ago
1.5 gave the same error.
on another note readByPrimaryKey seems to return an Object now instead of a Persistable?
I'm really not sure how to troubleshoot this, any hints would be very much appreciated.
Thanks,
Jeff
RE: Problem with Query Creation
-
Added by Oliver Gierke 5 months ago
Could you please check that the QueryLookupStrategy is not set to create in you current XML configuration. This would bypass the lookup of declared queries entirely (just wanna make sure it's not one of the more obvious issues ;).
Jeffrey Cutler wrote:
Which class and method checks to see if the named query exists? Perhaps I could add some logging to it to try and figure out what is going on.
Try raising the log level for org.synyx.hades to debug and you should get detailed reports in which steps the query is derived. I'd expect you see log output for trying to detect @Quer annotation followed by one looking for the named query. In your case you should also find the next step which is the creation then.
You could also try to configure the lookup strategy to use-named-query which insists on only using declared queries (either through @Query or a named query). As customizing this on the XML has effects on all your DAOs I recommend to create a small test case, that looks something like this:
1 EntityManager em = … // use injected EntityManager
2
3 GenericDaoFactory factory = GenericDaoFactory.create(em);
4 factory.setQueryLookupStrategy(QueryLookupStrategy.USE_DECLARED_QUERY);
5
6 SponsorDao dao = factory.getDao(SponsorDao.class);
It's crucial that you use the EntityManager you use in your setup as it is used for named query lookup and I suspect the error in that direction as the named query lookup seems to fail (otherwise Hades wouldn't try to create the query itself).
on another note
readByPrimaryKeyseems to return an Object now instead of aPersistable?
Entities are not required to implement Persistable anymore. So if you do not need that interface just type your DAO interface to a plain @Entity annotated class. Besides that you can still implement Persistable. What do you exactly mean with "returns an Object"? Of course it should return an object but of the type you declared at your DAO interface.
HTH,
Ollie
RE: Problem with Query Creation
-
Added by Jeffrey Cutler 5 months ago
Ollie,
I figured out the problem and it has nothing to Hades. The JPA entity annotations were incorrect in a couple of my sub-classes. The problem wasn't even in the Sponsor class, but that's where it manifested itself. It was a tough one to troubleshoot.
Thanks for your help and your quick response. Some good came out of it as I understand the internals of Hades a little better.
Regards,
Jeff