Results 1 to 5 of 5

Thread: Strange mySQL query

  1. #1
    Join Date
    Jun 2007
    Posts
    4

    Default 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()};

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

    Code:
    select estudiante0_.id 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:

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

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    		"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping auto-import="true" default-lazy="false">
        
        <class name="seminario.Estudiante" table="estudiante">
            <id name="id" column="id">
                <generator class="identity"/>
    	</id>
    	<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"/>
        </class>
    </hibernate-mapping>
    Still don't know why hibernate appends a "0_" in the query

    Thanks in advance and sorry for my poor English!

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jun 2007
    Location
    Melbourne, Australia
    Posts
    14

    Default

    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. #4
    Join Date
    Jun 2007
    Posts
    4

    Default

    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. #5
    Join Date
    Jun 2007
    Location
    canton,china,
    Posts
    7

    Default

    OK, Ok, that's ok,
    You can ignore it, that's hibernate alias.
    To the world you are just one person, but to one person you might be the whole world.
    --------------------
    http://silentwong.javaeye.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •