2012-03-22 68 views
0

UserEmployee相關爲一對多。
問題是PeristentSet爲空的Employee s的列表。
它從調試中看到。
而且至少有一個Employee存在於數據庫中User一對多hibernate映射:相關集合爲空

SQL:

create table users(
     id int not null primary key, 
... 
constraint `emp_constr` foreign key(`empid`) references `employee`(`id`)); 

用戶豆:

@ManagedBean 
@SessionScoped 
public class User { 
    private long id; 
    private List<Employee> employees = new ArrayList<Employee>(); 
... 

user.hbm.xml配置:

<list name="employees" cascade="all" inverse="false" fetch="join"> 
     <key> 
      <column name="userid" not-null="true"/> 
     </key> 
     <index column="idx"/> 
    <one-to-many class="entry.Employee"/> 
</list> 

DAO電話:

@Transactional 
public List<User> getUsers() { 
    return sessionFactory.getCurrentSession().createCriteria(User.class) 
      .list(); 
} 

.hbm.xml文件中是否有錯誤或者我應該明確添加setFetchMode()DAO

編輯:

我可以得到一個尺寸的集合而已,真正的它包含了這樣的配置更多的元素:

<list name="employees" table="employee" lazy="false"> 
     <key column="userid" not-null="true"/> 
    <list-index column="idx"/> 
    <one-to-many class="entry.Employee"/> 
</list> 

這是因爲idx=0
如果idx=7集合的大小將是8

回答

1

這是一個經典案例Lazy loading在休眠。

<list name="employees" .... lazy="false" > 

您需要的lazy="false"添加到列表中的映射。閱讀here獲取hibernate參考。

+0

我已經試過這個配置,它是相同的,相關集合的大小是0. – sergionni 2012-03-22 15:39:34

+0

ManuPK,請參閱編輯部分,請現在我可以獲取相關的集合只有一個元素,但實際上它包含3. – sergionni 2012-03-22 16:44:59

+0

試試用** **或** **代替**清單**。假設你有** idx ** 0,5,8的條目,那麼位置1列表中的項目將爲空。 – ManuPK 2012-03-23 03:25:09