Hi all,
Please forgive me for this long post, I hope it can be useful yet.
While talking about the freemarker datamodel provided for use in templates (in JIRA), max wrote:
Quote:
cfg = org.hibernate.mapping.Configuration
pojo = PojoClass
c2j = Cfg2JavaTool
c2h = Cfg2HbmTool
version = tool version number
date = DateTool
templates = used to e.g. create a file from a ftl template
importContext is something that is very much exporter and context dependent and not something that should be "global".
e.g. pojo's importContext is for that class and not for others. so if you need an additional importcontext you just create one.
I'll try to list a few use cases for (ant) hbmtemplate tool
(tell me if where I'm wrong):
- The exporter is given a filepattern, which, in case of java classes generation, may imply a specific destination package.
- The template may be applied to differents contexts, each related to a mapped class; this is actually specified by using the "{class-name}" marker in the file pattern
- the template may be dedicated to the pojo class definition. Then the destination package is specified in the hibernate mapping (the tool output directory being considered as an element of the classpath).
- or (dao example) class-dependant tools, which may be located in different package than the pojo, but maybe related to it (this would be the role of the "{package-name}" marker).
- or to an model-wide tool, accessing whole hibernate class mapping. For example the DAOFactory from Christian Bauer (http://www.hibernate.org/328.html).
So, the java import context may well be different from the pojo context, whilst it may be related to.
Now, here's what I understood (from the source code) for the tools we have:
- cfg = org.hibernate.mapping.Configuration
This gives access to global hibernate configuration as well as individual mappings.
- c2j = Cfg2JavaTool
This provides some methods to help writing java code.
But since it has no attribute, all methods could as well be static, except ...
see later ... - pojo = PojoClass
This wraps the mapped class or component from cfg, providing some more java-helpers. Amongst others, it holds an importContext attribute,
which is refered to for the importType() and generateImports() features.
It is initialized with the mapped class package.
I don't understand the semantics of the relationship between Cfg2JavaTool and PojoClass:
The BasicPOJOClass holds an instance of this (attribute-less) Cfg2JavaTool class, which has to be supplied by constructor:
Code:
public BasicPOJOClass(MetaAttributable ma, Cfg2JavaTool c2j) {
Further, the only
non-"static"-able, or instance-dependant, methods of Cfg2JavaTool are those POJOClass factories:
Code:
public POJOClass getPOJOClass(Component comp) {
return new ComponentPOJOClass(comp, this);
}
public POJOClass getPOJOClass(PersistentClass comp) {
return new EntityPOJOClass(comp, this);
}
Thus, most Cfg2JavaTool methods could be made static, and the POJOClass could as well drop the association and call class methods instead, couldn't it ?
I believe this state is due to historical reasons; thanks to the hibernate team, the project's code is living :)
*But*, I thought that since the Cfg2JavaTool() class was providing some java type related methods, like
Code:
public String asFinderArgumentList(Map parameterTypes, PersistentClass clazz);
it could as well be the home for the importContext, allowing to shorten class names relatively to *current* package, eventually different from pojo's.
The PojoClass could as well relay the importContext-related methods to its associated c2j.
I tried it, providing a new constructor:
Code:
public Cfg2JavaTool(String basePackage) {
importContext = new ImportContextImpl(basePackage);
}
Another one could be useful:
Code:
public Cfg2JavaTool() {
importContext = new NoopImportContext();
}
But dangerous as well, because it hides the problem of restoring the default behavior: use the mapped class package.
So the patch is not complete, but I'd like your opinion.
Congratulations for reading ;)
Michelle Baert
Brest, France