使用hibernate检查用户是否存在于数据库中

时间:2015-02-19 21:48:37

标签: java mysql hibernate

我是hibernate的新手

我有一个UserDAO类和一个确定用户是否存在于特定表中的方法。

这是users数据库中的hb3表:

enter image description here

这是我的班级:

public static void main(String[] args) {
    System.out.println(userExistsinDB("ABC"));
}

public static boolean userExistsinDB(String username) {
    String queryStr = "Select * from users where username=" + username; // since username is unique
    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();

    Query query = session.createSQLQuery(queryStr);
    System.out.println(query.getFirstResult());

    session.getTransaction().commit();
    session.close();
    sessionFactory.close();

    return false;
}

但结果是null query.getFirstResult(),为什么?

我在用户表中有ABC个用户名。

Hibernate config file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hb3</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">2323</property>

        <property name="hibernate.show_sql">true</property>
        <mapping class="sajjad.htlo.User"/>
    </session-factory>
</hibernate-configuration>

结果:

Feb 20, 2015 1:19:23 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Feb 20, 2015 1:19:23 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.1.Final}
Feb 20, 2015 1:19:23 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Feb 20, 2015 1:19:23 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Feb 20, 2015 1:19:23 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Feb 20, 2015 1:19:23 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Feb 20, 2015 1:19:23 AM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Feb 20, 2015 1:19:23 AM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Feb 20, 2015 1:19:23 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Feb 20, 2015 1:19:23 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hb3]
Feb 20, 2015 1:19:23 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
Feb 20, 2015 1:19:23 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Feb 20, 2015 1:19:23 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Feb 20, 2015 1:19:23 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Feb 20, 2015 1:19:23 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Feb 20, 2015 1:19:23 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
null
Feb 20, 2015 1:19:24 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/hb3]
false

2 个答案:

答案 0 :(得分:4)

试试这个:

org.hibernate.Query query = session.createQuery("from users where username = :username");
query.setParameter("username", username);
query.uniqueResult();

答案 1 :(得分:0)

您应该使用参数化查询。它返回null,因为字符串文字应该用引号表示法括起来。