2015-12-02 53 views
1

我對Hibernate,所以有可能會出現很多問題,但我已經顯示Account實體這裏:Hibernate查詢返回一個空的結果列表

@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" />控制檯中顯示)時,我得到返回結果。

我在做什麼錯?

編輯:

我想創建一個春天的Web應用程序,所以我連接的休眠在這裏的春天配置:

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

CHAGE您的查詢「從賬戶中選擇一個」到「從賬戶」 –

+0

@sumantipparapu這不會幫助 - 他使用JPA實體管理'JPQL' - 不是Hibernate的具體'HQL' –

+0

@Algosub如何你評估該清單是空的嗎? SQL的外觀如何?你是否檢查過你沒有使用Hibernate的其他數據庫?你用'create'使用'hibernate.hbm2ddl.auto'嗎?根據您提供的信息很難確定問題出在哪裏。 –

回答

0

發現問題, 我用錯了ojdbc驅動程序,不支持我的確切oracle數據庫版本,所以有些事情工作,有些則沒有。

感謝您在評論中提供的所有幫助!

+0

你是怎麼知道你的db需要什麼驅動的? –

+0

查看驅動程序文檔中支持的版本 –

+0

在嘗試查找兼容的驅動程序之前,您需要知道正在使用的數據庫。你可以使用SELECT * FROM V $ VERSION來獲取版本,然後谷歌搜索驅動程序並閱讀文檔。 –