2011-06-17 34 views
1

這是我在這裏的第一篇文章,希望我做對了。讓我看看如何更好地解釋我的問題,以便你們能理解它。數據庫查詢後使用空對象(使用hibernate + smartgwt)

[使用:Eclipse中,休眠,SmartGWT的,Java語言]

其中,我在我的數據庫(PostgreSQL系統),這是關係到一個另外2臺。

  • OrganizerUsers(organizeruser_id [PK],USER_ID,organizer_id [FK]) - 那些FKS表格2點其他不重要的相關。
  • 會議(conference_id [PK],標題,organizeruser_id [FK])

基本上,我需要在會議表中的所有行。所以我期待在查詢之後,我應該得到類似於Set<Conferences>的東西,集合中的每個對象都包含表中的信息。 現在,我使用Hibernate和Smartgwt來構建這個Web應用程序(對他們來說是新的)。 我的映射文件是在Eclipse中自動創建的。我的類文件也是由Hibernate代碼生成自動創建的。 注意到休眠執行以下一點:Set<Conferences>內的每個對象是會議對象,它有3個屬性:

  • INT conference_id
  • 字符串標題
  • 一個Organizeruser對象

最後一個對象基本上是取自OrganizerUsers表中的相應對象或者會議之間的關係主辦單位表 - 基本上進行了聯合操作。

查詢:

@SuppressWarnings("unchecked") 
    @Override 
    public List getConferences() throws IllegalArgumentException { 

     Session session = HibernateUtil.getSessionFactory().openSession(); 
     Transaction transaction = null; 
     List conferences = null; 
     try{ 
      transaction = session.beginTransaction(); 
      conferences = session.createQuery("from Conferences").list(); 
      transaction.commit(); 
     }catch(Exception e){ 
      transaction.rollback(); 
      e.printStackTrace(); 
     } 
     finally { 
      session.close(); 
     } 

     return conferences; 
    } 

結果通過一個RPC(在SmartGWT的)發送給客戶端(網頁)。這個上面的查詢基本上是一種servlet的一部分。

其結果是:

客戶機接收到Set<Conferences>會議對象。而在網頁中輸出數據,我注意到:

Set<Conferences> conferences = [the result from the RPC call]; 
conferences.getTitle() //works fine, returns title 
conferences.getConferenceId() //works fine, returns the id 
conferences.getOrganizeruser() //**NULL OBJECT** 

confereces.getOrganizeruser()應返回一個有效的對象,從organizeruser表中獲取。那裏有一個相應的有效行。

這只是一個例子,但無論我在哪裏與兩個表之間的FK有關係,我都會遇到這個問題。 在該內置HQL編輯器中爲相同示例執行HQL查詢似乎通過o_O [在Hibernate配置選項卡中]提供了正確的信息。是因爲休眠還是.. SmartGwt RPC調用?我對此表示懷疑。 BY THE WAY之前,我有一個類似的問題,在查詢後輸出空集。我通過熱切地取得它而不是懶惰(默認)來修復它。但我沒有得到這個工作。

附加信息: Conferences.hbm.xml映射文件[編輯 - 切出不重要的東西,並翻譯它寫在RO的類名稱]

<hibernate-mapping> 
    <class name="com.test.project.shared.Conferences" table="conferences" schema="public"> 
     <id name="conferenceId" type="int"> 
      <column name="conference_id" /> 
      <generator class="assigned" /> 
     </id> 
     <many-to-one name="organizerusers" class="com.test.project.shared.Organizerusers" fetch="select"> 
      <column name="organizerusers_id" not-null="true" /> 
     </many-to-one> 
     <property name="title" type="string"> 
      <column name="title" /> 
     </property> 
    </class> 
</hibernate-mapping> 

Conference.java類[編輯 - 剪下不重要的東西,並翻譯了RO中寫的類名]

@Entity 
@Table(name = "conferences", schema = "public") 
public class Conferinte extends LightEntity implements java.io.Serializable { 

    private int conferenceId; 
    private Organizerusers organizerusers; 
    private String title; 

    public Conferences() { 
    } 

    public Conferences(int conferenceId, Organizerusers organizerusers{ 
     this.confereceId = conferenceId; 
     this.organizerusers = organizerusers; 
    } 

    public Conferences(int conferenceId, Organizerusers organizerusers, String title){ 
     this.conferenceId = conferenceId; 
     this.organizerusers = organizerusers; 
     this.title = title; 
    } 

    @Id 
    @Column(name = "conference_id", unique = true, nullable = false) 
    public int getConferenceId() { 
     return this.conferenceId; 
    } 

    public void setConferenceId(int conferenceId) { 
     this.conferenceId = conferenceId; 
    } 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "organizeruser_id", nullable = false) 
    public Organizerusers getOrganizerusers() { 
     return this.organizerusers; 
    } 

    public void setOrganizerusers(
      Organizerusers organizerusers) { 
     this.organizerusers = organizerusers; 
    } 

    @Column(name = "title") 
    public String getTitle() { 
     return this.title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

} 

回答

0

This link幫助我解決了我的問題。所以如果有人有類似的問題,我建議你從那裏開始閱讀(太糟糕了,我以前沒有找到它)。