在配置文件中定义query
xml: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="onlyfun.caterpillar.User" table="USER"> <id name="id" type="string"> <column name="user_id" sql-type="char(32)" /> <generator class="uuid.hex"/> </id> <property name="name" type="string" not-null="true"> <column name="name" length="16" not-null="true"/> </property> <property name="sex" type="char"/> <property name="age" type="int"/> </class> <query name="onlyfun.caterpillar.queryUser"> <![CDATA[ select user.name from User as user where user.age = :age and user.sex = :sex ]]> </query> </hibernate-mapping> code Query query = session.getNamedQuery("onlyfun.caterpillar.queryUser"); query.setInteger("age", 25); query.setCharacter("sex", 'M');