Announcement Announcement Module
No announcement yet.
Strange mySQL query Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Strange mySQL query

    Hi all.

    I'm having troubles with hibernate. From a simple query like (hbtpl is a HibernateTemplate class)

    String parametros[] = {usuario.getLogin(), usuario.getPassword()};

    List resultado = hbtpl.find("from Estudiante estudiante where estudiante.login = ? and estudiante.password = ?",parametros);
    the mysql Log shows:

    select as id0_, estudiante0_.login as login0_, estudiante0_.password as password0_, estudiante0_.nombre as nombre0_, estudiante0_.telefono as telefono0_, estudiante0_.mail as mail0_, estudiante0_.direccion as direccion0_, estudiante0_.universidad as universi8_0_ from estudiante estudiante0_ where estudiante0_.login='the-login' and estudiante0_.password='the-password'
    Part of my application-context is:

        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource"><ref bean="dataSource"/></property>
    	<property name="mappingResources">
            <property name="hibernateProperties">
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
    And seminario.hbm.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    <hibernate-mapping auto-import="true" default-lazy="false">
        <class name="seminario.Estudiante" table="estudiante">
            <id name="id" column="id">
                <generator class="identity"/>
    	<property name="login" column="login"/>
    	<property name="password" column="password"/>
    	<property name="nombre" column="nombre"/>
    	<property name="telefono" column="telefono"/>
    	<property name="mail" column="mail"/>
    	<property name="direccion" column="direccion"/>
    	<property name="universidad" column="universidad"/>
    Still don't know why hibernate appends a "0_" in the query

    Thanks in advance and sorry for my poor English!

  • #2
    It's the way hibernate works it has it's own mechanism to determine unique propertynames.

    However I wonder your problem is? The fact that hibernate generates those queries I wouldn't worry about (try to figure out a query with multiple joins/subselects generated by hibernate).


    • #3
      I don't understand, is the above throwing some error?

      or are you just worried about the column/table alias names in the native SQL that hibernate is generating?

      If it is just the latter then don't worry - that is simply hibernate's way of making sure that the column/table aliases are kept unique in the query. While in you simple example it may not be obvious to you why hibernate does this, try and think about more complex queries - especially queries where you'd need to do a table self-join - in more complex cases like that you need to make sure that the column & table aliases you use are unique, adding a 0_ (and possibly 1_, 2_ etc when using more tables in the query) is simply hibernates way of dealing with this.

      Ultimately, all this is hibernate's internal way of generating SQL, as long as the results are what you're expecting them to be, you shouldn't have to worry about it.


      • #4
        My mistake. I was using this code with a login/password site and the mySQL table was empty . As the login failed, I thought it was the SQL query. Now I inserted a field and the query works fine.

        Thanks again. I'm embarrased about this stupid problem


        • #5
          OK, Ok, that's ok,
          You can ignore it, that's hibernate alias.