[編輯:很顯然,這只是針對陣列的問題和FoxyBOA的答案可能直接到(甚至IS)的答案。]爲什麼關聯的集合包含空值? (休眠,註釋,春季)
我的問題涉及到這些軟件:Hibernate3的+註釋,Spring MVC,MySQL,在這個例子中還有Spring Security。
我在想,爲什麼Hibernate自動關聯的集合爲子表的每個行號(除了正確的元素外)都包含空值。我的例子:
我有一個用戶和當局表中,用戶表是用戶名用作外鍵的主鍵。目前,我的權威表中有13行。當我從數據庫中檢索(MySQL的InnoDB的)用戶和Hibernate自動檢索與此對應的映射用戶的權限:
@OneToMany
@JoinColumn(name = "username")
@IndexColumn(name="id") // "id" was the primary key and is used to sort the elements
public Authority[] getAuthorities() {
return authorities;
}
public void setAuthorities(Authority[] authorities) {
this.authorities = authorities;
}
...我結束了一個集「部門」含14(0-13)其中只有四個元素不爲null(數據庫表中的四行屬於該特定用戶,因此這是正確的)。至於我意識到,我使用Hibernate的默認像Fetchmode等我收到這樣的用戶屬性:
Criteria criteria = getSession().createCriteria(User.class);
criteria.add(Restrictions.eq("username",username));
User user = (User) criteria.uniqueResult();
從org.hibernate.loader.loader正確的日誌記錄信息「中提到」爲四行結果集。但是,創建的用戶在數組中有四個正確的元素加上十個空值。在我的具體例子中,這導致此例外:
java.lang.IllegalArgumentException: Granted authority element 0 is null - GrantedAuthority[] cannot contain any null elements
我添加並刪除了一些索引,但仍然在與我的id列中的值相同的那些數組索引處獲得了具有四個正確值的14個值。所以我真的認爲這是我的問題所在。你能否再詳細說明一下,我並不完全理解你的意思。非常感謝! – Wolfram 2009-05-26 18:21:22