Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hibernate示例查询格式不正确_Java_Hibernate - Fatal编程技术网

Java Hibernate示例查询格式不正确

Java Hibernate示例查询格式不正确,java,hibernate,Java,Hibernate,User.hbm.xml <class name="dataAccess.User" table="USER" schema="PUBLIC" catalog="XYZ"> <id name="userId" type="long"> <column name="USER_ID" precision="10" scale="0" /> <generator class="assigned" /> &l

User.hbm.xml

<class name="dataAccess.User" table="USER" schema="PUBLIC" catalog="XYZ">
    <id name="userId" type="long">
        <column name="USER_ID" precision="10" scale="0" />
        <generator class="assigned" />
    </id>
    <property name="userName" type="string">
        <column name="USER_NAME" length="25" not-null="true" unique="true" />
    </property>
    <property name="emailId" type="string">
        <column name="EMAIL_ID" length="50" not-null="true" />
    </property>
    <property name="password" type="string">
        <column name="PASSWORD" length="50" not-null="true" />
    </property>
    <property name="admin" type="char">
        <column name="ADMIN" length="1" not-null="true" />
    </property>
    <property name="lastLoginTime" type="timestamp">
        <column name="LAST_LOGIN_TIME" length="23" />
    </property>

我只传递User类6个属性中的两个(username和password)。但在查询中,它还检查admin字段。如果我在标准中添加一个排除(“admin”),它就可以正常工作。我不太明白为什么会这样

给定admin字段的映射,我们可以假设它是boolean类型。布尔值要么为真,要么为假,但绝不为空。为什么该字段的值不是示例的一部分?Hibernate不能假设false表示“我想忽略这个字段”,而true表示“我想找到管理员标志为true的用户”。您很可能会查找管理员标志为false的用户。

我发现了问题


char的默认值为“\u0000”。仅当发现值为null时,创建条件才会排除该字段。如果我将其更改为String,它工作正常。

是在用户类设置中还是在更改admin字段?例如提供默认值的getter?@StormcrowSX No不是。我从表单中获取用户名密码,将其填充到用户对象中,并将其传递给方法。我尝试打印出对象中每个属性的值。只有username和password有值,其余的都是null。我已经将admin字段定义为character而不是boolean。将其更改为object字符而不是primitive字符可能也会起作用。一般来说,在Hibernate中更容易完全避免使用原语。
public List findByExample(User instance) {
    log.debug("finding User instance by example");
    try {
        List results = sessionFactory.openSession()
                .createCriteria("dataAccess.User")
                .add(Example.create(instance)).list();
        System.out.println(results.size());
        log.debug("find by example successful, result size: "
                + results.size());
        return results;
    } catch (RuntimeException re) {
        log.error("find by example failed", re);
        throw re;
    }
}