2012-10-12 8 views
0

我已經創建了一個示例Web應用程序。我使用MS SQL Server 2008作爲數據庫和hibernate +註釋來訪問數據庫。我的休眠配置xml如下。 問題是,Criteria.list()返回一個空列表,而且我看到'?'在生成的HSQL中,而不是我在標準中傳遞的參數。Hibernate Criteria創建一個'?'在生成的SQL

<session-factory name=""> 
    <property name="hibernate.connection.driver_class">sun.jdbc.odbc.JdbcOdbcDriver</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property> 
    <property name="hibernate.connection.url">jdbc:odbc:dbname</property> 

    <property name="connection.pool_size">10</property> 
    <property name="current_session_context_class">thread</property> 

    <!-- Disable the second-level cache --> 
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>  

    <!-- Echo all executed SQL to stdout --> 
    <property name="show_sql">true</property>  
    <property name="hibernate.connection.username"></property> 
    <property name="hibernate.connection.password"></property> 

<mapping class="com.demo.Person" /> 
</session-factory> 

這是我的註解豆

@Entity 
    @Table(name = "person") 
    public class Person implements Serializable { 

     public Person(){ 

     } 
     private static final long serialVersionUID = 1L; 
     @Id 
     @GeneratedValue(strategy=GenerationType.AUTO) 
     @Basic(optional = false) 
     @Column(name = "personid") 
     private Integer personid; 

     @Basic(optional = false) 
     @Column(name = "firstname") 
     private String firstname; 

     @Column(name = "lastname") 
     private String lastname; 

     @Basic(optional = false) 
     @Column(name = "phone") 
     private String phone; 

     @Column(name = "mobile") 
     private String mobile; 

     @Column(name = "street") 
     private String street; 

     @Basic(optional = false) 
     @Column(name = "city") 
     private String city; 

     @Basic(optional = false) 
     @Column(name = "country") 
     private String country; 

     @Basic(optional = false) 
     @Column(name = "bussinessowner") 
     private int bussinessowner; 

     @OneToMany(cascade = CascadeType.ALL, mappedBy = "resultid1") 
     private Collection<Recent> recentCollection; 

//setters & getters 
} 

而我運行的代碼是

Session session; 
     List list = new ArrayList(); 
     try{ 
     session = HibernateUtil.getSessionFactory().openSession(); 
     Criteria criteria = session.createCriteria(Person.class); 
     criteria.add(Restrictions.like("firstname", name, MatchMode.START)); 
     list= criteria.list(); 
     System.out.println(list.size()); 

     }catch (Exception e) { 
      e.printStackTrace(); 
     } 

生成HSQL:

Hibernate: select this_.personid as personid2_0_, this_.city as city2_0_, this_.country as country2_0_, this_.firstname as firstname2_0_, this_.lastname as lastname2_0_ from person this_ where this_.firstname like ? 

除此之外,我也沒有得到任何例外。你能幫我解決這個問題嗎? 謝謝!

+0

代碼中似乎沒有錯誤。你是否手動將查詢寫入數據庫,是否返回任何結果? – NPKR

+0

是的,如果我在管理工作室中執行查詢,它會返回結果。但在上面的代碼中,它返回的是空列表。 – Rakesh

+2

「?」是準備好的聲明 - 這裏沒有錯誤。當你手動執行查詢時,你會替換什麼?「用? –

回答

0

是否將其更改爲criteria.add(Restrictions.like("firstname", name + "%"));有幫助? 另外,?jbdc語句的正確參數佔位符。

+0

我試過了,但是即使這個方法不起作用 – Rakesh

+0

我試過同樣的使用Query並對查詢進行硬編碼,這樣它就可以正常工作。但使用'Criteria'不會取得任何結果! – Rakesh

+0

你確定你在一個數據庫實例上運行這兩個查詢嗎?也許它指向不同的數據庫,那就是你可以得到不同的結果。 – user1516873

0

代替此行

criteria.add(Restrictions.like("firstname", name, MatchMode.START)); 

嘗試的

criteria.add(Restrictions.like("firstname", name, MatchMode.ANYWHERE)); 

OR

criteria.add(Restrictions.ilike("firstname", name, MatchMode.ANYWHERE)); 

它將gerenerate SQL作爲姓LIKE '%ABCD%',如果你通過名字as「abcd」

你能簡單地嘗試這個

session = HibernateUtil.getSessionFactory().openSession(); 
List<Person> personList = session.createQuery("Select * from Person p where p.firstname like 'rake%'").list(); 

System.out.println("Person result :" + personList .size()); 
+0

我試圖使用查詢和硬編碼查詢相同,這種方式工作正常。但使用Criteria不會獲取任何結果! – Rakesh

+0

你有沒有試過上面發佈的ilike或ANYWHERE? –

+0

ANYWHERE也會返回一個空列表:( – Rakesh

0

@Rakesh試試下面的代碼:

criteria.add(Restrictions.ilike("firstname", name +"%")); 
+0

typecast問題試過之前,但沒有工作:( – Rakesh

0

'?'是JDBC語句中參數的佔位符。你看不到它的實際價值,就是這樣。它沒有問題,只是跟蹤與休眠仍然不完整。

如果你想看到'?'的實際值,那麼你必須使用額外的產品,如log4jdbc