有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Hibernate返回空列表而不是引发异常

我只是担心,为什么在使用来自http://www.tutorialspoint.com/hibernate/hibernate_criteria_queries.htm的Hibernate标准时

Criteria cr = session.createCriteria(Employee.class);
cr.add(Restrictions.eq("salary", 2000));
List results = cr.list();

若在我的数据库中Employee类并没有salary列,或者更糟的是Employee并没有映射,那个么结果将是一个空列表,hibernate不会抛出错误

我认为这是隐式多态性的问题,但首先我没有扩展任何内容,其次如果我使用session。删除,会话。保存,会话。更新一切正常,我得到了所有这些异常,比如NoSuchFieldException、UnknownEntity等等

但是,在使用条件获取对象时—只是空列表。 这是它应该的样子吗?我是否可以检查我正在搜索的类/字段是否存在于数据库中

StackOverflow上所有关于它的问题都与这个问题有关,当条件返回空列表时,它们在数据库中有对象。 我想知道为什么我的条件返回空列表,而它应该抛出错误

我的hibernate配置文件: `

<?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.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">******</property>
        <property name="hibernate.connection.url">*******</property>
        <property name="hibernate.connection.username">******</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.search.autoregister_listeners">false</property>
        <property name="hibernate.show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="use_sql_comments">true</property>
        <mapping resource="employee.hbm.xml" />
    </session-factory>
</hibernate-configuration>

`


共 (0) 个答案