Hello
I am using Hibernate and I noticed alot of select queries showed up where it is not necassary.
Code:
007-07-13 14:32:13 LOG: duration: 0.000 ms statement: select accessrigh0_.accessright_id as accessri1_6_0_, accessrigh0_.accessright_name as accessri2_6_0_ from accessright accessrigh0_ where accessrigh0_.accessright_id=46
2007-07-13 14:32:13 LOG: duration: 0.000 ms statement: select accessrigh0_.accessright_id as accessri1_6_0_, accessrigh0_.accessright_name as accessri2_6_0_ from accessright accessrigh0_ where accessrigh0_.accessright_id=59
2007-07-13 14:32:13 LOG: duration: 0.000 ms statement: select accessrigh0_.accessright_id as accessri1_6_0_, accessrigh0_.accessright_name as accessri2_6_0_ from accessright accessrigh0_ where accessrigh0_.accessright_id=45
2007-07-13 14:32:13 LOG: duration: 0.000 ms statement: select accessrigh0_.accessright_id as accessri1_6_0_, accessrigh0_.accessright_name as accessri2_6_0_ from accessright accessrigh0_ where accessrigh0_.accessright_id=51
2007-07-13 14:32:13 LOG: duration: 0.000 ms statement: select accessrigh0_.accessright_id as accessri1_6_0_, accessrigh0_.accessright_name as accessri2_6_0_ from accessright accessrigh0_ where accessrigh0_.accessright_id=57
2007-07-13 14:32:13 LOG: duration: 0.000 ms statement: select accessrigh0_.accessright_id as accessri1_6_0_, accessrigh0_.accessright_name as accessri2_6_0_ from accessright accessrigh0_ where accessrigh0_.accessright_id=40
2007-07-13 14:32:13 LOG: duration: 0.000 ms statement: select accessrigh0_.accessright_id as accessri1_6_0_, accessrigh0_.accessright_name as accessri2_6_0_ from accessright accessrigh0_ where accessrigh0_.accessright_id=35
2007-07-13 14:32:13 LOG: duration: 0.000 ms statement: select accessrigh0_.accessright_id as accessri1_6_0_, accessrigh0_.accessright_name as accessri2_6_0_ from accessright accessrigh0_ where accessrigh0_.accessright_id=39
2007-07-13 14:32:13 LOG: duration: 0.000 ms statement: select accessrigh0_.accessright_id as accessri1_6_0_, accessrigh0_.accessright_name as accessri2_6_0_ from accessright accessrigh0_ where accessrigh0_.accessright_id=36
2007-07-13 14:32:13 LOG: duration: 0.000 ms statement: select accessrigh0_.accessright_id as accessri1_6_0_, accessrigh0_.accessright_name as accessri2_6_0_ from accessright accessrigh0_ where accessrigh0_.accessright_id=38
2007-07-13 14:32:13 LOG: duration: 0.000 ms statement: select accessrigh0_.accessright_id as accessri1_6_0_, accessrigh0_.accessright_name as accessri2_6_0_ from accessright accessrigh0_ where accessrigh0_.accessright_id=63
This comes from a many-to-many mapping table, with one select on one side of the many-to-many table.
It does these selects for every many to many record in the table.
I can not enter a fetch="join" in my key-many-to-one files so how to solve this?
Mapping files:
Profile:Code:
<hibernate-mapping>
<class name="com.rdt.orm.Profile" table="profile">
<id name="profileId" column="profile_id" type="int">
<generator class="sequence">
<param name="sequence">profile_profile_id_seq</param>
</generator>
</id>
<set name="profileAccessRights" cascade="save-update, delete" inverse="true" lazy="true" >
<key column="profile_id" />
<one-to-many class="ProfileAccessRight" />
</set>
<property name="profileName" column="profile_name" type="string" />
<property name="profileLevel" column="profile_level" type="integer" />
</class>
</hibernate-mapping>
ProfileAccessRightCode:
<hibernate-mapping>
<class name="ProfileAccessRight" table="profileaccessright">
<composite-id>
<key-many-to-one name="profile" class="Profile" column="profile_id" lazy="proxy" />
<key-many-to-one name="accessRight" class="AccessRight" column="accessright_id" lazy="false" />
</composite-id>
<property name="profileAccessRightValue" column="profileaccessright_value" type="string" />
</class>
</hibernate-mapping>
AccessRightCode:
<hibernate-mapping>
<class name="AccessRight" table="accessright">
<id name="accessRightId" column="accessright_id" type="integer">
<generator class="sequence">
<param name="sequence">accessright_accessright_id_seq</param>
</generator>
</id>
<property name="accessRightName" column="accessright_name" type="string" />
</class>
</hibernate-mapping>
These are simplified mapping files but they contain all that is needed.
For example when a user logs in, i do this query:
Code:
...
User tempUser = (User) session.createQuery( "from User u where u.userName = '"+ user.getUserName() +"'").uniqueResult();
if( tempUser != null)
{
Hibernate.initialize( tempUser.getProfile().getProfileAccessRights());
}
...
This works fine but it does around 30 select queries for access rights, do i have to make a seperate query for selecting the accessrights or is there something else?
thanks