Hi everyone,
I am currently trying to generate methods on a DAO Home, for each named query that is defined on a JPA entity.
Unfortunately, the NamedQueryDefinition object that I get from Configuration.getNamedQueries(queryName) does not have a valid parameterTypes Map. The return Map is null, causing the my template to fail.
I searched the forum and found out another post that seems to be related (
viewtopic.php?f=6&t=1001745&p=2422898).
Furthermore, I also tried to put the query outside the class definition, on an orm.xml file, without success.
My question is: does Hibernate Tools support JPQL named queries and is it able to interpret them in order to extract their parameter types?
The error I'm facing is (I've omitted package names on my classes):
Code:
[hibernatetool] Executing Hibernate Tool with a JPA Configuration
[hibernatetool] 1. task: generic exporterclass: <...>.EAOExportertemplate: eaohome.ftl
15 [main] INFO org.hibernate.cfg.annotations.Version - Hibernate Annotations 3.4.0.GA
01-mar-2010 12:48:36 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.3
01-mar-2010 12:48:36 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
01-mar-2010 12:48:36 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
01-mar-2010 12:48:36 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
187 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.1.0.GA
187 [main] INFO org.hibernate.ejb.Version - Hibernate EntityManager 3.4.0.GA
390 [main] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: <...>.Contact
406 [main] INFO org.hibernate.cfg.annotations.QueryBinder - Binding Named query: Contact.findByEmail => SELECT c FROM Contact WHERE c.email= :email
453 [main] INFO org.hibernate.cfg.annotations.EntityBinder - Bind entity <...>.Contact on table CONTACT
500 [main] INFO org.hibernate.cfg.AnnotationConfiguration - Hibernate Validator not found: ignoring
01-mar-2010 12:48:36 org.hibernate.tool.Version <clinit>
INFO: Hibernate Tools 3.2.4.GA
01-mar-2010 12:48:36 freemarker.log.JDK14LoggerFactory$JDK14Logger error
GRAVE:
Error on line 45, column 1 in eaohome.ftl
cfg.namedQueries.get(queryName).parameterTypes is undefined.
It cannot be assigned to params
The problematic instruction:
----------
==> assignment: params=cfg.namedQueries.get(queryName).parameterTypes [on line 45, column 1 in eaohome.ftl]
----------
Java backtrace for programmers:
----------
freemarker.core.InvalidReferenceException: Error on line 45, column 1 in eaohome.ftl
cfg.namedQueries.get(queryName).parameterTypes is undefined.
It cannot be assigned to params
at freemarker.core.Assignment.accept(Assignment.java:111)
at freemarker.core.Environment.visit(Environment.java:196)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:196)
at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
at freemarker.core.Environment.visit(Environment.java:196)
at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:160)
at freemarker.core.Environment.visit(Environment.java:351)
at freemarker.core.IteratorBlock.accept(IteratorBlock.java:95)
at freemarker.core.Environment.visit(Environment.java:196)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:196)
at freemarker.core.Environment.visit(Environment.java:233)
at freemarker.core.BlockAssignment.accept(BlockAssignment.java:83)
at freemarker.core.Environment.visit(Environment.java:196)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:196)
at freemarker.core.Environment.process(Environment.java:176)
at freemarker.template.Template.process(Template.java:232)
at org.hibernate.tool.hbm2x.TemplateHelper.processTemplate(TemplateHelper.java:255)
at org.hibernate.tool.hbm2x.TemplateProducer.produceToString(TemplateProducer.java:67)
at org.hibernate.tool.hbm2x.TemplateProducer.produce(TemplateProducer.java:28)
at org.hibernate.tool.hbm2x.TemplateProducer.produce(TemplateProducer.java:103)
at org.hibernate.tool.hbm2x.GenericExporter.exportPOJO(GenericExporter.java:148)
at org.hibernate.tool.hbm2x.GenericExporter.exportPersistentClass(GenericExporter.java:137)
at org.hibernate.tool.hbm2x.GenericExporter$2.process(GenericExporter.java:43)
at org.hibernate.tool.hbm2x.GenericExporter.doStart(GenericExporter.java:128)
at org.hibernate.tool.hbm2x.AbstractExporter.start(AbstractExporter.java:95)
at org.hibernate.tool.ant.ExporterTask.execute(ExporterTask.java:40)
at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:186)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at ise.antelope.tasks.IfTask.doIf(IfTask.java:298)
at ise.antelope.tasks.IfTask.execute(IfTask.java:267)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:357)
at org.apache.maven.plugin.antrun.AbstractAntMojo.executeTasks(AbstractAntMojo.java:118)
at org.apache.maven.plugin.antrun.AntRunMojo.execute(AntRunMojo.java:98)
at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:556)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:535)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
[hibernatetool] An exception occurred while running exporter #2:generic exporterclass: <...>.EAOExportertemplate: eaohome.ftl
[hibernatetool] To get the full stack trace run ant with -verbose
[hibernatetool] org.hibernate.tool.hbm2x.ExporterException: Error while processing Entity: <...>.Contact with template eaohome.ftl
[hibernatetool] freemarker.core.InvalidReferenceException: Error on line 45, column 1 in eaohome.ftl
[hibernatetool] cfg.namedQueries.get(queryName).parameterTypes is undefined.
[hibernatetool] It cannot be assigned to params
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An Ant BuildException has occured: org.hibernate.tool.hbm2x.ExporterException: Error while processing Entity: <...>.Contact with template eaohome.ftl
Error on line 45, column 1 in eaohome.ftl
cfg.namedQueries.get(queryName).parameterTypes is undefined.
It cannot be assigned to params
Here's an example entity:
Code:
@Entity
@Table(name = "CONTACT")
@NamedQuery(name = "Contact.findByEmail", query = "SELECT c FROM Contact WHERE c.email= :email")
public class Contact {
...
}
And here's the part of the template that is failing:
Code:
<#assign params = cfg.namedQueries.get(queryName).parameterTypes>
This line was borrowed from daohome.ftl, which is able to generate Hibernate-specific methods, based on named queries.
I looked and the NamedQueryDefinition object is not null, unlike its parameter types map.
Environment data:
Hibernate Tools - 3.2.4GA
Hibernate Core - 3.2.3GA
Hibernate EntityManager - 3.4.0GA
Hibernate Annotations - 3.4.0GA
Hibernate Commons Annotations - 3.1.0GA
Thanks in advance.
Regards,
Nuno Guerreiro