Hibernate查询返回空结果列表

时间:2015-12-02 09:58:38

标签: java spring oracle hibernate

我是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(在<entry key="hibernate.show_sql" value="true" />中显示在控制台中)时,我会得到结果。

我做错了什么?

修改

我正在尝试创建一个spring web应用程序,所以我的连接是在春天配置一个hibernate:

<?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>

1 个答案:

答案 0 :(得分:0)

发现问题, 我使用了错误的ojdbc驱动程序,该驱动程序不支持我的oracle数据库版本,因此有些功能正常,有些则没有。

感谢所有评论方面的帮助!