Announcement Announcement Module
No announcement yet.
[HHH] Support for Config.addClas() in Hibernate 3 Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • [HHH] Support for Config.addClas() in Hibernate 3

    Hibernate 3 Config class has a nifty way to find out mapping files based on class names; call Config.addClas(Class clazz) and it will look for a mapping file on the same directory as that clazz, suffixed by ".hbm.xml".

    So, what I'd love to see in spring is:

    <bean id="sessionFactory" class="LocalSessionFactoryBean">
      <property name="mappingClasses">
    You could argue that "mappingResources" is almost the same, except that you use "/" instead of ".", and you have to explicitely add the ".hbm.xml" suffix. That would be true, if I didn't use IDEA for my day-to-day programming. When I move or rename a class in IDEA, it will not only properly refactor the references in java code, but will also figure out references to the fully qualified class name in non-java (let's say, our lovely application context) and rename them.

    If you use IDEA, you know what I'm talking about, and feel my pain. If you don't, you have no idea (no pun intended) how useful this little feature would be.

    EDIT: for removing duplicate words words.

  • #2
    Alternative solution

    As a side note, I'm currently circumventing this problem by subclassing LocalSessionFactoryBean to add a "mappingClasses" property that delegates to the "mappingResources" property:

    public class ClassAwareLocalSessionFactoryBean extends LocalSessionFactoryBean
        public void setMappingClasses&#40;Class&#91;&#93; mappingClasses&#41;
            String&#91;&#93; mappingResources = new String&#91;mappingClasses.length&#93;;
            for &#40;int i = 0; i < mappingClasses.length; i++&#41;
                mappingResources&#91;i&#93; = mappingClasses&#91;i&#93;.getName&#40;&#41;.replace&#40;'.', '/'&#41; + ".hbm.xml";
    This works, but it would be nicer if instead of manually translating the class name to a mapping file, LocalSessionFactoryBean actually acknowledged a collection of classes to pass to Config.addClass(), and let Hibernate figure out the resource locations.