2013-03-10 34 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

相关问题