2013-03-10 75 views
0

我比較新,嘗試使用一對多映射加載子實體。但是,在初始化過程中,我看到只有一個實體被加載。關聯的子表還有3個行沒有加載,雖然我看到返回4行的查詢是正確的。 Hibernate沒有初始化。hibernate:一對多不加載所有實體,但只加載一個

如果您在日誌跟蹤中看到粗體,則會看到第一行被初始化,但在此之後,沒有剩餘的行被初始化。 不知道是什麼問題。請提供一些有用的指針。非常感謝。

-Ron

下面是代碼片斷:

EMP table; 
EMP_ACCT table; 

    EMP_ID ACCT_TYPE   ACCT_VAL 

------ ---------   -------- 

1001 checking  test val 

1001 savings   test val1 

1001 high yield savings test val2 

1001 simple checking test val3 


    <!-- EMP table --> 
    <hibernate-mapping> 
    <class name="com.Employee" table="EMP" dynamic-update="true"> 
     <id name="empId" type="java.lang.Integer"> 
      <column name="EMP_ID" precision="5" scale="0" /> 
      <generator class="assigned"></generator>   
     </id> 
     <property name="empName" type="java.lang.String"> 
      <column name="EMP_NAME" length="100"/> 
     </property>   
     <set name="empAcctDetails" inverse="false" lazy="false" table="EMP_ACCT" 
         cascade="all" fetch="subselect"> 
      <key> 
       <column name="EMP_ID" /> 
      </key> 
      <one-to-many class="com.EmployeeAcctDetails" /> 
     </set> 
    </class> 
    </hibernate-mapping> 

    <!-- EMP_ACCT table --> 
    <hibernate-mapping> 
    <class name="com.EmployeeAcctDetails" table="EMP_ACCT" dynamic-update="true"> 

     <id name="empId" type="java.lang.Integer"> 
      <column name="EMP_ID" precision="5" scale="0" /> 
      <generator class="assigned"></generator>   
     </id> 
     <property name="acctName" type="java.lang.String"> 
      <column name="ACCT_NAME" length="20"/> 
     </property>   
     <property name="acctValue" type="java.lang.String"> 
       <column name="ACCT_VALUE" length="30" /> 
     </property> 

     <many-to-one name="emp" class="com.domain.Employee" update="false" insert="false"> 
      <column name="EMP_ID" /> 
     </many-to-one> 

    </class> 

    </hibernate-mapping> 


Log trace: 
--------- 

2013-03-10 12:13:29,558 DEBUG [org.hibernate.SQL]- 
    /* load one-to-many com.domain.Employee.empAcctDetails */ select 
     empacctdet0_.EMP_ID as EMP1_1_, 
     empacctdet0_.EMP_ID as EMP1_81_0_, 
     empacctdet0_.ACCT_NAME as ACCT2_81_0_, 
     empacctdet0_.ACCT_VALUE as ACCT3_81_0_ 
    from 
     EMP_ACCT empacctdet0_ 
    where 
     empacctdet0_.EMP_ID=? 
Hibernate: 
    /* load one-to-many com.domain.Employee.empAcctDetails */ select 
     empacctdet0_.EMP_ID as EMP1_1_, 
     empacctdet0_.EMP_ID as EMP1_81_0_, 
     empacctdet0_.ACCT_NAME as ACCT2_81_0_, 
     empacctdet0_.ACCT_VALUE as ACCT3_81_0_ 
    from 
     EMP_ACCT empacctdet0_ 
    where 
     empacctdet0_.EMP_ID=? 
2013-03-10 12:13:29,558 TRACE [org.hibernate.jdbc.AbstractBatcher]- preparing statement 
2013-03-10 12:13:29,604 TRACE [org.hibernate.type.IntegerType]- binding '1001' to parameter: 1 
2013-03-10 12:13:29,683 DEBUG [org.hibernate.jdbc.AbstractBatcher]- about to open ResultSet (open ResultSets: 0, globally: 0) 
2013-03-10 12:13:29,683 DEBUG [org.hibernate.loader.Loader]- result set contains (possibly empty) collection: [com.domain.Employee.empAcctDetails#1001] 
2013-03-10 12:13:29,683 TRACE [org.hibernate.engine.loading.LoadContexts]- constructing collection load context for result set [[email protected]] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.engine.loading.CollectionLoadContext]- starting attempt to find loading collection [[com.domain.Employee.empAcctDetails#1001]] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.engine.loading.CollectionLoadContext]- collection not yet initialized; initializing 
2013-03-10 12:13:29,698 TRACE [org.hibernate.loader.Loader]- processing result set 
2013-03-10 12:13:29,698 DEBUG [org.hibernate.loader.Loader]- result set row: 0 
2013-03-10 12:13:29,698 TRACE [org.hibernate.type.IntegerType]- returning '1001' as column: EMP1_81_0_ 
2013-03-10 12:13:29,698 DEBUG [org.hibernate.loader.Loader]- result row: EntityKey[com.domain.EmployeeAcctDetails#1001] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.loader.Loader]- Initializing object from ResultSet: [com.domain.EmployeeAcctDetails#1001] 
**2013-03-10 12:13:29,698 TRACE [org.hibernate.persister.entity.AbstractEntityPersister]- Hydrating entity: [com.domain.EmployeeAcctDetails#1001] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.type.StringType]- returning 'checking' as column: ACCT2_81_0_ 
2013-03-10 12:13:29,698 TRACE [org.hibernate.type.StringType]- returning 'test val' as column: ACCT3_81_0_ 
2013-03-10 12:13:29,698 TRACE [org.hibernate.type.IntegerType]- returning '1001' as column: EMP1_81_0_ 
2013-03-10 12:13:29,698 TRACE [org.hibernate.type.IntegerType]- returning '1001' as column: EMP1_1_** 
2013-03-10 12:13:29,698 DEBUG [org.hibernate.loader.Loader]- found row of collection: [com.domain.Employee.empAcctDetails#1001] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.engine.loading.CollectionLoadContext]- starting attempt to find loading collection [[com.domain.Employee.empAcctDetails#1001]] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.engine.loading.LoadContexts]- attempting to locate loading collection entry [CollectionKey[com.domain.Employee.empAcctDetails#1001]] in any result-set context 
2013-03-10 12:13:29,698 TRACE [org.hibernate.engine.loading.LoadContexts]- collection [CollectionKey[com.domain.Employee.empAcctDetails#1001]] located in load context 
2013-03-10 12:13:29,698 TRACE [org.hibernate.engine.loading.CollectionLoadContext]- found loading collection bound to current result set processing; reading row 
2013-03-10 12:13:29,698 TRACE [org.hibernate.type.IntegerType]- returning '1001' as column: EMP1_1_ 
2013-03-10 12:13:29,698 TRACE [org.hibernate.event.def.DefaultLoadEventListener]- loading entity: [com.domain.EmployeeAcctDetails#1001] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.event.def.DefaultLoadEventListener]- attempting to resolve: [com.domain.EmployeeAcctDetails#1001] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.event.def.DefaultLoadEventListener]- resolved object in session cache: [com.domain.EmployeeAcctDetails#1001] 
2013-03-10 12:13:29,698 DEBUG [org.hibernate.loader.Loader]- result set row: 1 
2013-03-10 12:13:29,698 TRACE [org.hibernate.type.IntegerType]- returning '1001' as column: EMP1_81_0_ 
2013-03-10 12:13:29,698 DEBUG [org.hibernate.loader.Loader]- result row: EntityKey[com.domain.EmployeeAcctDetails#1001] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.type.IntegerType]- returning '1001' as column: EMP1_1_ 
2013-03-10 12:13:29,698 DEBUG [org.hibernate.loader.Loader]- found row of collection: [com.domain.Employee.empAcctDetails#1001] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.engine.loading.CollectionLoadContext]- starting attempt to find loading collection [[com.domain.Employee.empAcctDetails#1001]] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.engine.loading.LoadContexts]- attempting to locate loading collection entry [CollectionKey[com.domain.Employee.empAcctDetails#1001]] in any result-set context 
2013-03-10 12:13:29,698 TRACE [org.hibernate.engine.loading.LoadContexts]- collection [CollectionKey[com.domain.Employee.empAcctDetails#1001]] located in load context 
2013-03-10 12:13:29,698 TRACE [org.hibernate.engine.loading.CollectionLoadContext]- found loading collection bound to current result set processing; reading row 
2013-03-10 12:13:29,698 TRACE [org.hibernate.type.IntegerType]- returning '1001' as column: EMP1_1_ 
2013-03-10 12:13:29,698 TRACE [org.hibernate.event.def.DefaultLoadEventListener]- loading entity: [com.domain.EmployeeAcctDetails#1001] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.event.def.DefaultLoadEventListener]- attempting to resolve: [com.domain.EmployeeAcctDetails#1001] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.event.def.DefaultLoadEventListener]- resolved object in session cache: [com.domain.EmployeeAcctDetails#1001] 
2013-03-10 12:13:29,698 DEBUG [org.hibernate.loader.Loader]- result set row: 2 
2013-03-10 12:13:29,698 TRACE [org.hibernate.type.IntegerType]- returning '1001' as column: EMP1_81_0_ 
2013-03-10 12:13:29,698 DEBUG [org.hibernate.loader.Loader]- result row: EntityKey[com.domain.EmployeeAcctDetails#1001] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.type.IntegerType]- returning '1001' as column: EMP1_1_ 
2013-03-10 12:13:29,698 DEBUG [org.hibernate.loader.Loader]- found row of collection: [com.domain.Employee.empAcctDetails#1001] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.engine.loading.CollectionLoadContext]- starting attempt to find loading collection [[com.domain.Employee.empAcctDetails#1001]] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.engine.loading.LoadContexts]- attempting to locate loading collection entry [CollectionKey[com.domain.Employee.empAcctDetails#1001]] in any result-set context 
2013-03-10 12:13:29,698 TRACE [org.hibernate.engine.loading.LoadContexts]- collection [CollectionKey[com.domain.Employee.empAcctDetails#1001]] located in load context 
2013-03-10 12:13:29,698 TRACE [org.hibernate.engine.loading.CollectionLoadContext]- found loading collection bound to current result set processing; reading row 
2013-03-10 12:13:29,698 TRACE [org.hibernate.type.IntegerType]- returning '1001' as column: EMP1_1_ 
2013-03-10 12:13:29,698 TRACE [org.hibernate.event.def.DefaultLoadEventListener]- loading entity: [com.domain.EmployeeAcctDetails#1001] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.event.def.DefaultLoadEventListener]- attempting to resolve: [com.domain.EmployeeAcctDetails#1001] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.event.def.DefaultLoadEventListener]- resolved object in session cache: [com.domain.EmployeeAcctDetails#1001] 
2013-03-10 12:13:29,698 DEBUG [org.hibernate.loader.Loader]- result set row: 3 
2013-03-10 12:13:29,698 TRACE [org.hibernate.type.IntegerType]- returning '1001' as column: EMP1_81_0_ 
2013-03-10 12:13:29,698 DEBUG [org.hibernate.loader.Loader]- result row: EntityKey[com.domain.EmployeeAcctDetails#1001] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.type.IntegerType]- returning '1001' as column: EMP1_1_ 
2013-03-10 12:13:29,698 DEBUG [org.hibernate.loader.Loader]- found row of collection: [com.domain.Employee.empAcctDetails#1001] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.engine.loading.CollectionLoadContext]- starting attempt to find loading collection [[com.domain.Employee.empAcctDetails#1001]] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.engine.loading.LoadContexts]- attempting to locate loading collection entry [CollectionKey[com.domain.Employee.empAcctDetails#1001]] in any result-set context 
2013-03-10 12:13:29,698 TRACE [org.hibernate.engine.loading.LoadContexts]- collection [CollectionKey[com.domain.Employee.empAcctDetails#1001]] located in load context 
2013-03-10 12:13:29,698 TRACE [org.hibernate.engine.loading.CollectionLoadContext]- found loading collection bound to current result set processing; reading row 
2013-03-10 12:13:29,698 TRACE [org.hibernate.type.IntegerType]- returning '1001' as column: EMP1_1_ 
2013-03-10 12:13:29,698 TRACE [org.hibernate.event.def.DefaultLoadEventListener]- loading entity: [com.domain.EmployeeAcctDetails#1001] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.event.def.DefaultLoadEventListener]- attempting to resolve: [com.domain.EmployeeAcctDetails#1001] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.event.def.DefaultLoadEventListener]- resolved object in session cache: [com.domain.EmployeeAcctDetails#1001] 
2013-03-10 12:13:29,698 TRACE [org.hibernate.loader.Loader]- done processing result set (4 rows) 

回答

0

你定義EMP_ID作爲實體EmployeeAcctDetails的主鍵列。這沒有任何意義,因爲相應表中有多行具有相同的EMP_ID值。

將一個真正的主鍵列添加到表中,並將其用作主鍵列。

+0

感謝您的回覆和澄清。 – ron0102 2013-03-12 14:59:35

相關問題