简体   繁体   中英

Hibernate query returns empty result list

I am new to Hibernate, so there might be many problems, but I have an Account entity displayed here:

@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;
    }
}

and I am trying to select all accounts in the database in the following function:

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

The problem is that this returns an empty list, but when run the result sql (shown in the console by <entry key="hibernate.show_sql" value="true" /> ) in the Oracle SQL Developer editor, I do get results back.

What am I doing wrong?

Edit:

I am trying to create a spring web app, so my connection an hibernate are configured in spring here:

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

Found the problem, I used the wrong ojdbc driver that did not support my exact oracle database version, so some things worked, and some didn't.

Thanks for all the help in comments!

I also faced the exact same issue with hibernate. For some tables, the generated Hibernate SQL query was not returning data back from DB in my service but when I ran it directly in my SQL Developer I got the data. For some tables, everything was working fine. It turned out to be an issue with how I was inserting the data into the tables. I needed to write a commit statement explicitly after inserting the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM