Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:3.0.5
Mapping documents:
Code:
<class name="User" table="MM_USERS">
<id name="id" column="id" type="int" unsaved-value="null"
length="5">
<generator class="native" />
</id>
<property name="name" column="name" type="string" length="50" not-null="true" unique="false" />
<set name="memberships" table="MM_USERGROUPLINKS" cascade="none" order-by="name asc" lazy="true" inverse="false">
<key column="userID" />
<many-to-many class="project.vo.Group" column="groupID" />
</set>
</class>
Code between sessionFactory.openSession() and session.close():Code:
Transaction tx = session.beginTransaction();
Query query = session.createQuery("select u from User u order by u.name asc");
Iterator results = query.iterate();
while (results.hasNext()) {
User u = (User) results.next();
System.out.println("User: " + u.getId() + " " + u.getName());
}
tx.commit();
Full stack trace of any exception that occurs:
Name and version of the database you are using: SQL Server 2000
The generated SQL (show_sql=true):
Hibernate: select user0_.id as col_0_0_ from MM_USERS user0_ order by user0_.nam
e asc
Hibernate: select user0_.id as id0_, user0_.name as name2_0_ from MM_USERS user0
_ where user0_.id=?
Hibernate: select user0_.id as id0_, user0_.name as name2_0_ from MM_USERS user0
_ where user0_.id=?
User: 2 Ang Wee Leong
Hibernate: select user0_.id as id0_, user0_.name as name2_0_ from MM_USERS user0
_ where user0_.id=?
User: 4 Lai Boon Keng
Hibernate: select user0_.id as id0_, user0_.name as name2_0_ from MM_USERS user0
_ where user0_.id=?
User: 1 Lee See Meng
User: 3 Lek Hock Khoon
As per above, I have 4 records in the MM_USERS table. Why does Hibernate use 5 queries when using native SQL, only one is required??
Anyway to optimize this? I can't imagine what happens if I had 1000 records in the table...
Thanks!