论坛首页 Java企业应用论坛

如果查询的字段中有空的值,hibernate怎么查询不到所需要的糪\0...

浏览 18164 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2003-10-10  
没有关系。感谢你的回答
但从商业逻辑上讲,NULL好像是没用的
但是现在公司对很多表留有备用字段,所以null值在所难免.
我改动了些表结构:
------------------------------------------------------
logonid  emailadress  lastlogon  password  name 
1            1@y.com      2                22
11          4·@n.com     3                22
2    
3 
4            2@y.com 

如果我找找为"22"的用户名且密码为空的所以纪录(2条)
      String queryString = "select user from User as user where user.password = :pass and user.userName = :nam";
      net.sf.hibernate.Query query = session.createQuery(queryString);;
      query.setString("pass", null);;
      query.setString("nam", "22");;

好像也查不出啦

换个话讲是不是凡是表中有null的字段都不能通过查询语句来查询呢?
0 请登录后投票
   发表时间:2003-10-10  
没道理呀
select user from User as user where user.password is null and user.userName is null 

应该是可以的,请确定password,userName字段到底是空值还是空串。
0 请登录后投票
   发表时间:2003-10-10  
是空值null
相关代码在上面。你可以自己试试,做个简单的表就可以尝试查询了

另外还发现:如果User us =new User();
us.setName("11");
其他字段不用管的话。
发现数据库中这个记录其他字段均为null
0 请登录后投票
   发表时间:2003-10-10  
引用

另外还发现:如果User us =new User();
us.setName("11");
其他字段不用管的话。
发现数据库中这个记录其他字段均为null

不一定,这和hibernate的dynamic-insert设置以及数据库表相应字段是否有默认值有关。

引用

是空值null
相关代码在上面。你可以自己试试,做个简单的表就可以尝试查询了

在我的机器上用is null查询是没问题的(hibernate2.0.2 + mysql 4.0.15 inno DB)。
0 请登录后投票
   发表时间:2003-10-13  
to yehs220
可否提供一个你做实验用的demo
贴出这个demo的相关代码也可以:
可能我的hbm.xml文件中的一些属性没有配置好。我想看看你是这么做的。
0 请登录后投票
   发表时间:2003-10-13  
很简单
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

        <class name="h.A"
        	   table="A"
        	   proxy="h.A">                

                <id name="id" column="id" type="long">
                    <generator class="assigned">                		
        			</generator>
                </id>

                <property name="col1"/>

        </class>

</hibernate-mapping>


System.out.println(session.find("from A a where a.col1 is null"););;
0 请登录后投票
   发表时间:2003-10-13  
给你我的映射文件:帮忙看看有问题么?
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="dbdemo.User"
        table="Users"
        dynamic-update="false"
    >

        <id
            name="userID"
            column="LogonID"
            type="string"
            unsaved-value="any"
        >
            <generator class="assigned">
            </generator>
        </id>

        <property
            name="emailAddress"
            type="string"
            update="true"
            insert="true"
            column="EmailAddress"
        />

        <property
            name="lastLogon"
            type="date"
            update="true"
            insert="true"
            column="LastLogon"
        />

        <property
            name="password"
            type="string"
            update="true"
            insert="true"
            column="Password"
        />

        <property
            name="userName"
            type="string"
            update="true"
            insert="true"
            column="Name"
        />

        <set
            name="contacts"
            table="Contacts"
            lazy="false"
            inverse="false"
            cascade="all"
            sort="unsorted"
        >

              <key
                  column="User_ID"
              />

              <one-to-many
                  class="dbdemo.Contact"
              />
        </set>

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-User.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>





<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="dbdemo.Contact"
        table="Contacts"
        dynamic-update="false"
    >

        <id
            name="contactId"
            column="ID"
            type="long"
            unsaved-value="any"
        >
            <generator class="sequence">
                <param name="sequence">seq</param>
            </generator>
        </id>

        <property
            name="email"
            type="string"
            update="true"
            insert="true"
            column="EmailAddress"
        />

        <property
            name="name"
            type="string"
            update="true"
            insert="true"
            column="Name"
        />

        <many-to-one
            name="user"
            class="dbdemo.User"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="User_ID"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Contact.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>



还是没有琢磨出来那里有错
0 请登录后投票
   发表时间:2003-10-13  
映射应该没什么问题。

把show_sql打开,看看生成的sql应该就清楚了。
0 请登录后投票
   发表时间:2003-10-13  
是查看结果么?
我的程序代码为:
      String queryString = "select user from User as user where user.userName is :nam and user.password is :pass";
      net.sf.hibernate.Query query = session.createQuery(queryString);;
      query.setString("nam", null);;
      query.setString("pass", null);;
      System.out.println("---- list-1--------");;
      for (java.util.Iterator it = query.iterate();; it.hasNext();; ); {
        User us = (User); it.next();;
        System.out.println(us.getUserID(); + "   Password: " + us.getPassword(););;
      }
      System.out.println("--------------------------\r\n");;


出错信息为:就是搞不懂那里的问题
---- list-1--------
Hibernate: select user0_.LogonID as x0_0_ from db2admin.Users user0_ where (user0_.Name is ? );and(user0_.Password is ? );
ERROR [main] (JDBCExceptionReporter.java:46); - [IBM][CLI Driver][DB2/NT] SQL0104N  在 "_ where (user0_.Name" 之后发现意外的记号 "is"。期望的记号可能包括:"IN"。  SQLSTATE=42601
ERROR [main] (JDBCException.java:37); - Could not execute query
COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver][DB2/NT] SQL0104N  在 "_ where (user0_.Name" 之后发现意外的记号 "is"。期望的记号可能包括:"IN"。  SQLSTATE=42601
	at COM.ibm.db2.jdbc.net.SQLExceptionGenerator.throw_SQLException(SQLExceptionGenerator.java:244);
	at COM.ibm.db2.jdbc.net.SQLExceptionGenerator.check_return_code(SQLExceptionGenerator.java:408);
	at COM.ibm.db2.jdbc.net.SQLExceptionGenerator.check_return_code(SQLExceptionGenerator.java:386);
	at COM.ibm.db2.jdbc.net.DB2PreparedStatement.executeQuery(DB2PreparedStatement.java:973);
	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:179);
	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:179);
	at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:71);
	at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:551);
	at net.sf.hibernate.hql.QueryTranslator.iterate(QueryTranslator.java:805);
	at net.sf.hibernate.impl.SessionImpl.iterate(SessionImpl.java:1409);
	at net.sf.hibernate.impl.QueryImpl.iterate(QueryImpl.java:64);
	at dbdemo.HibernateDemoOneToMany.run(HibernateDemoOneToMany.java:153);
	at dbdemo.HibernateDemoOneToMany.main(HibernateDemoOneToMany.java:26);
COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver][DB2/NT] SQL0104N  在 "_ where (user0_.Name" 之后发现意外的记号 "is"。期望的记号可能包括:"IN"。  SQLSTATE=42601
	at COM.ibm.db2.jdbc.net.SQLExceptionGenerator.throw_SQLException(SQLExceptionGenerator.java:244);
	at COM.ibm.db2.jdbc.net.SQLExceptionGenerator.check_return_code(SQLExceptionGenerator.java:408);
	at COM.ibm.db2.jdbc.net.SQLExceptionGenerator.check_return_code(SQLExceptionGenerator.java:386);
	at COM.ibm.db2.jdbc.net.DB2PreparedStatement.executeQuery(DB2PreparedStatement.java:973);
	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:179);
	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:179);
	at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:71);
	at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:551);
	at net.sf.hibernate.hql.QueryTranslator.iterate(QueryTranslator.java:805);
	at net.sf.hibernate.impl.SessionImpl.iterate(SessionImpl.java:1409);
	at net.sf.hibernate.impl.QueryImpl.iterate(QueryImpl.java:64);
	at dbdemo.HibernateDemoOneToMany.run(HibernateDemoOneToMany.java:153);
	at dbdemo.HibernateDemoOneToMany.main(HibernateDemoOneToMany.java:26);
rethrown as net.sf.hibernate.JDBCException: Could not execute query: [IBM][CLI Driver][DB2/NT] SQL0104N  在 "_ where (user0_.Name" 之后发现意外的记号 "is"。期望的记号可能包括:"IN"。  SQLSTATE=42601
	at net.sf.hibernate.impl.SessionImpl.iterate(SessionImpl.java:1412);
	at net.sf.hibernate.impl.QueryImpl.iterate(QueryImpl.java:64);
	at dbdemo.HibernateDemoOneToMany.run(HibernateDemoOneToMany.java:153);
	at dbdemo.HibernateDemoOneToMany.main(HibernateDemoOneToMany.java:26);
Caused by: COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver][DB2/NT] SQL0104N  在 "_ where (user0_.Name" 之后发现意外的记号 "is"。期望的记号可能包括:"IN"。  SQLSTATE=42601
	at COM.ibm.db2.jdbc.net.SQLExceptionGenerator.throw_SQLException(SQLExceptionGenerator.java:244);
	at COM.ibm.db2.jdbc.net.SQLExceptionGenerator.check_return_code(SQLExceptionGenerator.java:408);
	at COM.ibm.db2.jdbc.net.SQLExceptionGenerator.check_return_code(SQLExceptionGenerator.java:386);
	at COM.ibm.db2.jdbc.net.DB2PreparedStatement.executeQuery(DB2PreparedStatement.java:973);
	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:179);
	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:179);
	at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:71);
	at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:551);
	at net.sf.hibernate.hql.QueryTranslator.iterate(QueryTranslator.java:805);
	at net.sf.hibernate.impl.SessionImpl.iterate(SessionImpl.java:1409);
	... 3 more
HibernateException Could not execute query: [IBM][CLI Driver][DB2/NT] SQL0104N  在 "_ where (user0_.Name" 之后发现意外的记号 "is"。期望的记号可能包括:"IN"。  SQLSTATE=42601
0 请登录后投票
   发表时间:2003-10-13  
我这样也没问题。
  Query query=session.createQuery("select a from A a where a.col1 is :v1 and a.col1 is :v2");;
        query.setString("v1",null);;
        query.setString("v2",null);;

        query.iterate();;

可能是jdbc的问题,我用的是mysql的数据库及驱动。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics