hibernate:一对多不加载所有实体但只加载一个

时间:2013-03-10 16:59:31

标签: hibernate hibernate-mapping

我比较新,并尝试使用一对多映射加载子实体。但是,在初始化过程中,我看到只加载了一个实体。相关的子表还有3行未加载但我看到查询得到4行,这是正确的。 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 [oracle.jdbc.driver.OracleResultSetImpl@23102310]
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)

1 个答案:

答案 0 :(得分:0)

您将EMP_ID定义为实体EmployeeAcctDetails的主键列。这没有任何意义,因为相应表中有几行具有相同的EMP_ID值。

向表中添加一个真正的主键列,并将其用作主键列。