0

I have 3 Tables - Lease Account, Lease, Transaction. Each Lease Account has N Leases, each Lease has N Transactions. I am allowed to do only 1 query to the database - Select * from Lease Account, to fetch all this data into Java as N Lease Account objects - each Lease Account object containing a of Lease objects - each Lease containing a of Transaction objects.

Can somebody please guide me on how to write the one to many mapping (hbm.xml) for this scenario in Hibernate(3.2) ?

Thanks.

Jay
  • 233
  • 2
  • 8

1 Answers1

0

In your lease account hbm xml

<class name="LeaseAccounts" table="lease">

    <id name="leaseAccountsId" type="java.lang.Long" column="id" >
        <generator class="native" />
    </id>

<set name="lease" table="lease_accounts"
                    inverse="true" lazy="true" fetch="select">
                <key>
                    <column name="id" not-null="true" />
                </key>
                <one-to-many class="Lease.class" />
            </set>


</class>

Similarly you can write for your lease table.
NB: Select * will load all data or not depends upon your lazy initialization. If lazy loading is set to true , then your need to do Hibernate.initialize(your collection)

(The lazy attribute tells hibernate when to get the children. The fetch attribute tells hibernate how to get the children.)

Community
  • 1
  • 1
pratim_b
  • 1,154
  • 9
  • 27