使用xml映射错误使用命名的本机sql查询

时间:2013-12-15 05:31:17

标签: hibernate hibernate-mapping named-query hbmxml

我正在使用xml映射的命名本机SQL查询,我需要使用mysql函数AES_ENCRYPT和AES_DECRYPT,但标记了我的语法错误,但不明白为什么。现在回顾一下,很好

我正在使用blob字段存储AES_ENCRYPT的加密数据

Usuario.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 14/12/2013 03:41:41 AM by Hibernate Tools 3.6.0 -->
<hibernate-mapping>
    <class name="Model.TO.Usuario" table="usuario" catalog="sintaxhighlighter">
        <id name="correo" type="string">
            <column name="Correo" length="50" />
            <generator class="assigned" />
        </id>
        <many-to-one name="roles" class="Model.TO.Roles" fetch="select">
            <column name="Roles_idRoles" length="2" not-null="true" />
        </many-to-one>
        <property name="password" type="java.lang.String">
            <column name="Password"  not-null="true" />
        </property>
        <property name="nombres" type="string">
            <column name="Nombres" length="50" not-null="true" />
        </property>
        <property name="apellidoP" type="string">
            <column name="ApellidoP" length="25" not-null="true" />
        </property>
        <property name="apellidoM" type="string">
            <column name="ApellidoM" length="25" not-null="true" />
        </property>
        <set name="codigoses" table="usuarios_has_codigos" inverse="false" lazy="true" fetch="select">
            <key>
                <column name="Usuarios_Correo" length="50" not-null="true" />
            </key>
            <many-to-many entity-name="Model.TO.Codigos">
                <column name="Codigos_CodName" length="4" not-null="true" />
            </many-to-many>
        </set>
    </class>

       <sql-query name="finByCorreo" >
        <return alias="Usuario" class="Model.TO.Usuario" />
        SELECT use.Correo AS {Usuario.correo},  
        AES_DECRYPT (use.Password,'escom') AS {Usuario.password},
        use.Nombres AS {Usuario.nombres}, 
        use.ApellidoP AS {Usuario.apellidoP}, 
        use.ApellidoM AS {Usuario.apellidoM}     
        FROM usuario use
        WHERE use.Correo LIKE :tags
    </sql-query>
</hibernate-mapping>

FindByCorreo

public Usuario finByCorreo(Usuario usuario) {
        Usuario model = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        System.out.println(usuario.getPassword() + " " + usuario.getCorreo());

        try {
            session.beginTransaction();
            Query query=session.getNamedQuery("finByCorreo");

            query.setString("tags", usuario.getCorreo());
            model = (Usuario) query.uniqueResult();
            session.getTransaction().commit();
        } catch (HibernateException e) {
            System.out.println(e.getMessage());
            session.getTransaction().rollback();
        }
        return model;
    }

Usuario.java

public class Usuario  implements java.io.Serializable {
     private String correo;
     private Roles roles;
     private String password;
     private String nombres;
     private String apellidoP;
     private String apellidoM;
     private Set codigoses = new HashSet(0);
     .
 }

导致错误

WARN: SQL Error: 1064, SQLState: 42000
dic 14, 2013 11:13:14 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE use.Correo LIKE 'amezcua92@hotmail.com'' at line 7

1 个答案:

答案 0 :(得分:0)

我在那里看到的一个问题是你的usuario表的别名。 use 可以是数据库引擎的关键字。如果您使用的是MySQL,请查看此9.3. Reserved Words

<sql-query name="finByCorreo" >
    <return alias="Usuario" class="Model.TO.Usuario" />
    SELECT ut.Correo AS {Usuario.correo},  
    AES_DECRYPT (ut.Password,'escom') AS {Usuario.password},
    ut.Nombres AS {Usuario.nombres}, 
    ut.ApellidoP AS {Usuario.apellidoP}, 
    ut.ApellidoM AS {Usuario.apellidoM}     

    FROM usuario ut ------------------- here ut instead of use

    WHERE ut.Correo LIKE :tags
</sql-query>