Announcement Announcement Module
Collapse
No announcement yet.
Isolate Hibernate Queries Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Isolate Hibernate Queries

    Hello I am trying to design an application using Spring 1.1.3 and Hibernate 2.1.7. I would like to put the hibernate queries in a hql.properties file; i saw that we could refere that file using something like:

    Code:
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    		<property name="basename"><value>hql</value></property>
    </bean>
    But I don't know how to access my queries from the code using the related keys. I watched quickly on the forum or in the reference but their is a missing part that would help me (TOTO: SHOW AN EXEMPLE in the section 3.11.1).

    Thx for any help.

  • #2
    Re: Isolate Hibernate Queries

    You could use a PropertyPlaceholderConfigurer to set query string properties based on the contents of a properties file. See Spring reference and API for info about PropertyPlaceholderConfigurer.

    BTW- I question the "conventional wisdom" of storing HQL queries outside of the DAO class. If the queries use parameters (as most of mine do), then you have a situation where the query and its parameters are stored in different locations. Spreading interrelated metadata across files often leads to bugs. Just my $0.02.

    Comment


    • #3
      Thx for your quick answer. If I understood well the use of PropertyPlaceholderConfigurer it seems to be more dedicated to the configuration files. In my case each time I want to add a query (pretty often) I have to set a new entry in the XML file.
      Code:
      <bean id="querySource" class="com.example.QuerySource">
        <property name="SELECT_ALL_MEMBER"><value>$&#123;SELECT_ALL_MEMBER&#125;</value></property>
      </bean>
      And another one in my hql.properties file like:
      Code:
      SELECT_ALL_MEMBER=From com.example.Member
      Maybe it is the proper way to do it but I am surprised that it is not possible to just refer the properties file in the bean and then be able to get the targeted query using the key SELECT_ALL_MEMBER from the code.

      Thx for any help.

      Comment


      • #4
        Well, you could also use Spring to set just the filename/URL of the properties file in your bean and then use java.util.Properties.load to initialize the properties object in your code.

        If you're using Hibernate, you could also use getNamedQuery in Session.

        Comment


        • #5
          Using Hibernate named queries (included in .hbm.xml) is the standard/recommended way by Hibernate. Spring's HibernateTemplate provides methods for using these queries:
          - findByNamedQuery
          - findByNamedQueryAndNamedParam
          HTH

          Comment

          Working...
          X