【问题标题】:Hibernate query returns empty result listHibernate 查询返回空结果列表
【发布时间】:2015-12-02 09:58:37
【问题描述】:

我是 Hibernate 的新手,所以可能会有很多问题,但我在这里显示了一个 Account 实体:

@Entity
@Table(name = "TABLE_NAME")
public class Account {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private int id;

    @Column(name = "EMAIL", unique = true)
    private String email;

    @Column(name = "PASSWORD", length = 128)
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

我正在尝试在以下函数中选择数据库中的所有帐户:

public List<Account> getAllAccounts() {
    Query q = em.createQuery("select a from Account a");
    return q.getResultList();
}

问题是这会返回一个空列表,但是当在 Oracle SQL Developer 编辑器中运行结果 sql(在控制台中由 &lt;entry key="hibernate.show_sql" value="true" /&gt; 显示)时,我确实得到了结果。

我做错了什么?

编辑:

我正在尝试创建一个 spring web 应用程序,所以我的连接和休眠是在 spring 中配置的:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.checkpoint.core.repositories.jpa" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@[[url]]:[[port]]:[[sid]]" />
        <property name="username" value="username" />
        <property name="password" value="password" />
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <map>
                <entry key="hibernate.hbm2ddl.auto" value="validate" />
                <entry key="hibernate.show_sql" value="true" />
                <entry key="hibernate.format_sql" value="true" />
                <entry key="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
            </map>
        </property>
        <property name="packagesToScan" value="com.checkpoint.core.models.entities" />
    </bean>

    <!-- Setup transaction management -->
    <tx:annotation-driven />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" />

    <context:component-scan base-package="com.checkpoint.core.services.impl" />

</beans>

【问题讨论】:

  • 将您的查询“从帐户 a 中选择一个”更改为“从帐户”
  • @sumantipparapu 这无济于事 - 他正在使用带有 JPQL 的 JPA 实体管理器 - 而不是特定于 Hibernate 的 HQL
  • @Algosub 您如何评估列表为空? SQL 的外观如何?您是否检查过您没有使用与 Hibernate 不同的数据库?您是否将hibernate.hbm2ddl.autocreate 一起使用?根据您提供的信息,很难判断问题出在哪里。
  • 您可以尝试使用:Query q = em.createQuery("select a from Account a", Account.class); 这应该可以解决问题。
  • @TobiasLiefke ,我将hibernate.hbm2ddl.autovalidate 一起使用,并检查返回的列表以确保它确实为空。我应该使用HQL吗?

标签: java spring oracle hibernate


【解决方案1】:

发现问题, 我使用了错误的 ojdbc 驱动程序,该驱动程序不支持我的确切 oracle 数据库版本,所以有些事情有效,有些则没有。

感谢 cmets 的所有帮助!

【讨论】:

  • 你是怎么知道你的数据库需要什么驱动的?
  • 在驱动文档中查看了支持的版本
  • 在尝试寻找兼容的驱动程序之前,您需要知道您正在使用的数据库。您可以使用 SELECT * FROM V$VERSION 来获取版本,然后谷歌搜索驱动程序并阅读文档。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
  • 2021-03-18
  • 2021-11-20
  • 2019-10-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多