Hibernate - ()之间的限制

时间:2014-09-16 13:21:45

标签: java hibernate hibernate-criteria restriction

 Criteria cr=session.createCriteria(Student.class).add(Restrictions.between("strStudentMark1", "90", "100"));

在上面的代码片段中,我给出了90到100之间的标记。在Student表中,标记90和100之间有字段,但是没有在输出中获得这些字段。

可能是什么问题?我犯了什么错误吗?

修改

是的,当我跑步时,我会得到整个清单。

Criteria cr=session.createCriteria(Student.class);

学生实体位于下方,生成了Getters and Setters。

import java.io.Serializable;

public class Student implements Serializable {

private String strStudentID;
private String strStudentRegNO;
private String strStudentName;
private String strStudentMark1;
private String strStudentMark2;
private String strStudentDegree;
private String strStudentMobileNO;
private String strStudentMailID;
private String strSalary;


public String getStrSalary() {
    return strSalary;
}
public void setStrSalary(String strSalary) {
    this.strSalary = strSalary;
}
public String getStrStudentID() {
    return strStudentID;
}
public void setStrStudentID(String strStudentID) {
    this.strStudentID = strStudentID;
}
public String getStrStudentRegNO() {
    return strStudentRegNO;
}
public void setStrStudentRegNO(String strStudentRegNO) {
    this.strStudentRegNO = strStudentRegNO;
}
public String getStrStudentName() {
    return strStudentName;
}
public void setStrStudentName(String strStudentName) {
    this.strStudentName = strStudentName;
}
public String getStrStudentMark1() {
    return strStudentMark1;
}
public void setStrStudentMark1(String strStudentMark1) {
    this.strStudentMark1 = strStudentMark1;
}
public String getStrStudentMark2() {
    return strStudentMark2;
}
public void setStrStudentMark2(String strStudentMark2) {
    this.strStudentMark2 = strStudentMark2;
}
public String getStrStudentDegree() {
    return strStudentDegree;
}
public void setStrStudentDegree(String strStudentDegree) {
    this.strStudentDegree = strStudentDegree;
}
public String getStrStudentMobileNO() {
    return strStudentMobileNO;
}
public void setStrStudentMobileNO(String strStudentMobileNO) {
    this.strStudentMobileNO = strStudentMobileNO;
}
public String getStrStudentMailID() {
    return strStudentMailID;
}
public void setStrStudentMailID(String strStudentMailID) {
    this.strStudentMailID = strStudentMailID;
}


}

hibernate.cfg.xml中。

<!DOCTYPE hibernate-configuration PUBLIC 
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>

    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="connection.url">jdbc:oracle:thin:@aaaa:bbbb</property>
    <property name="connection.username">xxx</property>
    <property name="connection.password">yyy</property>
    <!-- <property name="connection.pool_size">5</property> -->

       <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <!-- Echo all executed SQL to stdout -->

    <property name="show_sql">true</property>
    <property name="current_session_context_class">thread</property>

    <!-- <property name="hbm2ddl.auto">update</property> -->

    <mapping resource="com/hibresources/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Student.hbm.xml。

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 17 Dec, 2010 5:54:42 AM by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
<class name="com.bean.Student" table="STUDENT">
    <meta attribute="class-description">
        This class has details abt Students
    </meta>
    <id name="strStudentID" >
        <column name="STUID" />
    </id>
    <property name="strStudentRegNO"  >
        <column name="STUREG_NO" />
    </property>
    <property name="strStudentName" >
        <column name="STUNAME" />
    </property>
    <property name="strStudentMark1" >
        <column name="STUMARK1" />
    </property>

    <property name="strStudentMark2" >
        <column name="STUMARK2" />
    </property>
    <property name="strStudentDegree" >
        <column name="DEGREE" />
    </property>

    <property name="strStudentMobileNO" >
        <column name="MOBILENO" />
    </property>

    <property name="strStudentMailID" >
        <column name="MAILID" />
    </property>

    <property name="strSalary" >
        <column name="SALARY" />
    </property>

</class>
</hibernate-mapping>

1 个答案:

答案 0 :(得分:3)

请确保存储在数据库中的数据是INT。 Between仅适用于INT数据类型。还可以尝试在代码中将值作为int传递。