-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: lazy collections get fetched right away
PostPosted: Thu Mar 30, 2006 4:39 am 
Newbie

Joined: Wed Mar 01, 2006 4:14 am
Posts: 16
for starters I use hibernate 3.2.1, hibernate-annotations 3.1beta8, spring 1.2.6, I test with junit 3.8.1 and a MySql 5.0 database, I let the database scheme be generated by hibernate itself, testing uses the spring data access objects, the testers use this code in the setup and teardown

TestCase#setUp()
---------------------------------------------------------------------------
this.appContext = new ClassPathXmlApplicationContext("spring.xml");
this.sessionFactory = (SessionFactory) getBean("sessionFactory");
this.session = this.sessionFactory.openSession();
TransactionSynchronizationManager.bindResource(
this.sessionFactory, new SessionHolder(this.session)
);
---------------------------------------------------------------------------

TestCase#tearDown()
---------------------------------------------------------------------------
SessionHolder holder =
(SessionHolder) TransactionSynchronizationManager.getResource(
this.sessionFactory
);
this.session = holder.getSession();
session.flush();
TransactionSynchronizationManager.unbindResource(
this.sessionFactory
);
SessionFactoryUtils.releaseSession(
this.session, this.sessionFactory
);
---------------------------------------------------------------------------



one of the objects Location has a lot of references (4 many-to-many, 1 one-to-one, 1 many-to-one, 2 one-to-many) for now
---------------------------------------------------------------------------
@Entity
@Table(name = "INF_LOCATION")
public class Location implements Serializable {
.....
@NotNull
@ManyToOne(fetch = FetchType.LAZY, cascade = {
CascadeType.PERSIST}
)
@JoinColumn(name = "campus_id_ref")
private Campus campus;

@OneToOne(fetch = FetchType.LAZY, optional = true)
@PrimaryKeyJoinColumn
private FmisData fmisData;

/** Het materiaal dat op deze locatie beschikbaar is. */
@ManyToMany(fetch = FetchType.LAZY, cascade = {
CascadeType.PERSIST}
)
@JoinTable(name = "INF_LOCATION_EQUIPMENT", joinColumns = {
@JoinColumn(name = "location_id_ref")
}
, inverseJoinColumns = {
@JoinColumn(name = "equipment_id_ref")
}
)
@OrderBy(value = "name")
private Set<Equipment> equipment;
....
---------------------------------------------------------------------------


in FmisData
---------------------------------------------------------------------------
...
@OneToOne(fetch = FetchType.LAZY, optional = true)
@PrimaryKeyJoinColumn
private Location location;
...
---------------------------------------------------------------------------


in Campus
---------------------------------------------------------------------------
...
@OneToMany(fetch = FetchType.LAZY, mappedBy = "campus", cascade = {
CascadeType.PERSIST}
)
@OrderBy(value = "name")
private Set<Location> locations;...
---------------------------------------------------------------------------


in Equipment
---------------------------------------------------------------------------
...
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "equipment", cascade = {
CascadeType.PERSIST}
)
@OrderBy(value = "name")
private Set<Location> locations;
...
---------------------------------------------------------------------------





my problem is now that when a location is already in the database and i retrieve it by hibernatetemplate.load or .get that calls the session.load or get, all collections are retrieved resulting in 9 different selects instead of one, what do I do wrong? why isn't this lazy loading?





another problem, maybe it's releated (all objects have a
@Version
private Long version;
but I see updates passing by on a many to many relation even when nothing has changed except a relation has been made: like a location already exists, an equipment already exists, I add an equipment object to the relation set in location and vica versa






you get credits for solving the problem or helping me


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 30, 2006 5:03 am 
Newbie

Joined: Wed Mar 01, 2006 4:14 am
Posts: 16
some more info

--------------------------

all Sets are initialized in the each objects contructor, like this

this.equipment = Collections.synchronizedSet(
new LinkedHashSet<Equipment>()
);

I tried to remove that but that doesn't help


----------------------------

almost all collections above all empty

----------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 31, 2006 3:41 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
It should work.

Turn on debug log.
Watch all sql logs.

Set a break point just after session.get(Location.class, ID).
You should get just one query - with maybe some joins.
Attach all sql queries that arise from this session.get.

Do you maybe have some class inheritance?

Rgds, Ales


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 31, 2006 4:03 am 
Newbie

Joined: Wed Mar 01, 2006 4:14 am
Posts: 16
there is no class inheritance

but you are right: this is the whole code and logging; the other unexpected selects happen not when you get the location and group (many-to-many) but if you attach them to each other and save them, still aint sure why those other selects happen

Code:
            Location location =
                mockInfrastructureLookupServiceImpl.getLocation(34L);
            LocationGroup group =                mockInfrastructureLookupServiceImpl.getGroup(7L);
            location.addGroup(group);
            group.addLocation(location);
            mockInfrastructureAdminServiceImpl.saveLocation(location);


this is the log


Code:
10:08:05,968 DEBUG [TestInfrastructureAdminServiceImpl] setUp() - start
10:08:05,968 INFO  [SpringUnitContext] ====== START SETUP be.ugent.dgfb.cab.midtier.service.TestInfrastructureAdminServiceImpl ======
10:08:06,953 WARN  [PropertyPlaceholderConfigurer] Could not load properties from URL [file:/C:/Documents and Settings/jdlandsh/dgfb-cab.properties]: C:\Documents and Settings\jdlandsh\dgfb-cab.properties (Het systeem kan het opgegeven bestand niet vinden)
10:08:06,953 WARN  [PropertyPlaceholderConfigurer] Could not load properties from class path resource [webauth.properties]: class path resource [webauth.properties] cannot be opened because it does not exist
10:08:07,031 WARN  [PropertyPlaceholderConfigurer] Could not load properties from URL [file:/C:/Documents and Settings/jdlandsh/webauth.properties]: C:\Documents and Settings\jdlandsh\webauth.properties (Het systeem kan het opgegeven bestand niet vinden)
10:08:09,937 INFO  [ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
10:08:10,125 DEBUG [CacheManager] Creating new CacheManager with default config
10:08:10,140 DEBUG [CacheManager] Configuring ehcache from classpath.
10:08:10,156 WARN  [Configurator] No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/Documents%20and%20Settings/jdlandsh/.m2/repository/ehcache/ehcache/1.1/ehcache-1.1.jar!/ehcache-failsafe.xml
10:08:10,500 DEBUG [Configuration$DiskStore] Disk Store Path: C:\DOCUME~1\jdlandsh\LOCALS~1\Temp\
10:08:15,234 DEBUG [SpringUnitContext] getBean(String) - start
10:08:15,234 DEBUG [SpringUnitContext] getBean(String) - end
10:08:15,328 INFO  [SpringUnitContext] ====== END SETUP be.ugent.dgfb.cab.midtier.service.TestInfrastructureAdminServiceImpl ======
10:08:15,328 DEBUG [SpringUnitContext] getBean(String) - start
10:08:15,328 DEBUG [SpringUnitContext] getBean(String) - end
10:08:15,328 DEBUG [SpringUnitContext] getBean(String) - start
10:08:15,328 DEBUG [SpringUnitContext] getBean(String) - end
10:08:15,328 DEBUG [TestInfrastructureAdminServiceImpl] setUp() - end
10:08:15,328 DEBUG [TestInfrastructureAdminServiceImpl] testBatch4() - start
10:08:15,781 DEBUG [SQL]
    /* load be.ugent.dgfb.cab.model.infrastructure.LocationGroup */ select
        locationgr0_.id as id4_0_,
        locationgr0_.version as version4_0_,
        locationgr0_.description as descript3_4_0_,
        locationgr0_.name as name4_0_
    from
        INF_LOCATIONGROUP locationgr0_
    where
        locationgr0_.id=?
10:08:15,812 DEBUG [LongType] binding '7' to parameter: 1
10:08:15,828 DEBUG [LongType] returning '0' as column: version4_0_
10:08:15,828 DEBUG [StringType] returning 'group' as column: descript3_4_0_
10:08:15,828 DEBUG [StringType] returning 'group-name' as column: name4_0_
10:08:15,890 DEBUG [SQL]
    /* load collection be.ugent.dgfb.cab.model.infrastructure.LocationGroup.locations */ select
        locations0_.locationgroup_id_ref as location2_1_,
        locations0_.location_id_ref as location1_1_,
        location1_.id as id3_0_,
        location1_.city as city3_0_,
        location1_.postalCode as postalCode3_0_,
        location1_.street as street3_0_,
        location1_.streetNumber as streetNu5_3_0_,
        location1_.rentable as rentable3_0_,
        location1_.reservable as reservable3_0_,
        location1_.campus_id_ref as campus12_3_0_,
        location1_.maximumCapacity as maximumC8_3_0_,
        location1_.version as version3_0_,
        location1_.description as descrip10_3_0_,
        location1_.name as name3_0_
    from
        INF_LOCATION_GROUPS locations0_
    left outer join
        INF_LOCATION location1_
            on locations0_.location_id_ref=location1_.id
    where
        locations0_.locationgroup_id_ref=?
10:08:15,890 DEBUG [LongType] binding '7' to parameter: 1
10:08:15,890 DEBUG [LongType] returning '34' as column: id3_0_
10:08:15,890 DEBUG [StringType] returning null as column: city3_0_
10:08:15,890 DEBUG [StringType] returning null as column: postalCode3_0_
10:08:15,890 DEBUG [StringType] returning null as column: street3_0_
10:08:15,890 DEBUG [StringType] returning null as column: streetNu5_3_0_
10:08:15,890 DEBUG [BooleanType] returning 'true' as column: rentable3_0_
10:08:15,890 DEBUG [BooleanType] returning 'true' as column: reservable3_0_
10:08:15,890 DEBUG [LongType] returning null as column: campus12_3_0_
10:08:15,890 DEBUG [IntegerType] returning '10' as column: maximumC8_3_0_
10:08:15,890 DEBUG [LongType] returning '0' as column: version3_0_
10:08:15,890 DEBUG [StringType] returning 'desc' as column: descrip10_3_0_
10:08:15,890 DEBUG [StringType] returning 'NAME' as column: name3_0_
10:08:15,890 DEBUG [LongType] returning '7' as column: location2_1_
10:08:15,890 DEBUG [LongType] returning '34' as column: location1_1_
10:08:15,890 DEBUG [SQL]
    /* load be.ugent.dgfb.cab.model.infrastructure.fmis.FmisData */ select
        fmisdata0_.id as id9_0_,
        fmisdata0_.area as area9_0_,
        fmisdata0_.buildingId as buildingId9_0_,
        fmisdata0_.campusId as campusId9_0_,
        fmisdata0_.floorId as floorId9_0_,
        fmisdata0_.roomFullId as roomFullId9_0_,
        fmisdata0_.roomId as roomId9_0_,
        fmisdata0_.roomType as roomType9_0_
    from
        INF_FMIS_DATA fmisdata0_
    where
        fmisdata0_.id=?
10:08:15,906 DEBUG [LongType] binding '34' to parameter: 1
10:08:15,921 DEBUG [SQL]
    /* load collection be.ugent.dgfb.cab.model.infrastructure.Location.equipment */ select
        equipment0_.location_id_ref as location1_1_,
        equipment0_.equipment_id_ref as equipment2_1_,
        equipment1_.id as id1_0_,
        equipment1_.version as version1_0_,
        equipment1_.description as descript3_1_0_,
        equipment1_.name as name1_0_
    from
        INF_LOCATION_EQUIPMENT equipment0_
    left outer join
        INF_EQUIPMENT equipment1_
            on equipment0_.equipment_id_ref=equipment1_.id
    where
        equipment0_.location_id_ref=?
10:08:15,921 DEBUG [LongType] binding '34' to parameter: 1
10:08:15,921 DEBUG [SQL]
    /* load collection be.ugent.dgfb.cab.model.infrastructure.Location.groups */ select
        groups0_.location_id_ref as location1_1_,
        groups0_.locationgroup_id_ref as location2_1_,
        locationgr1_.id as id4_0_,
        locationgr1_.version as version4_0_,
        locationgr1_.description as descript3_4_0_,
        locationgr1_.name as name4_0_
    from
        INF_LOCATION_GROUPS groups0_
    left outer join
        INF_LOCATIONGROUP locationgr1_
            on groups0_.locationgroup_id_ref=locationgr1_.id
    where
        groups0_.location_id_ref=?
10:08:15,921 DEBUG [LongType] binding '34' to parameter: 1
10:08:15,921 DEBUG [LongType] returning '7' as column: id4_0_
10:08:15,921 DEBUG [LongType] returning '34' as column: location1_1_
10:08:15,921 DEBUG [LongType] returning '7' as column: location2_1_
10:08:15,937 DEBUG [SQL]
    /* load one-to-many be.ugent.dgfb.cab.model.infrastructure.Location.managers */ select
        managers0_.location_id_ref as location7_1_,
        managers0_.id as id1_,
        managers0_.id as id5_0_,
        managers0_.employee_id_ref as employee6_5_0_,
        managers0_.location_id_ref as location7_5_0_,
        managers0_.version as version5_0_,
        managers0_.managerType as managerT3_5_0_,
        managers0_.endDate as endDate5_0_,
        managers0_.startDate as startDate5_0_
    from
        INF_MANAGER managers0_
    where
        managers0_.location_id_ref=?
10:08:15,937 DEBUG [LongType] binding '34' to parameter: 1
10:08:15,937 DEBUG [SQL]
    /* load collection be.ugent.dgfb.cab.model.infrastructure.Location.notifications */ select
        notificati0_.location_id_ref as location1_1_,
        notificati0_.notification_id_ref as notifica2_1_,
        notificati1_.id as id6_0_,
        notificati1_.version as version6_0_,
        notificati1_.endDate as endDate6_0_,
        notificati1_.startDate as startDate6_0_,
        notificati1_.message as message6_0_
    from
        INF_LOCATION_NOTIFICATIONS notificati0_
    left outer join
        INF_NOTIFICATION notificati1_
            on notificati0_.notification_id_ref=notificati1_.id
    where
        notificati0_.location_id_ref=?
10:08:15,937 DEBUG [LongType] binding '34' to parameter: 1
10:08:15,937 DEBUG [SQL]
    /* load one-to-many be.ugent.dgfb.cab.model.infrastructure.Location.pictures */ select
        pictures0_.location_id_ref as location8_1_,
        pictures0_.id as id1_,
        pictures0_.id as id7_0_,
        pictures0_.height as height7_0_,
        pictures0_.width as width7_0_,
        pictures0_.location_id_ref as location8_7_0_,
        pictures0_.version as version7_0_,
        pictures0_.image_encoding as image5_7_0_,
        pictures0_.name as name7_0_,
        pictures0_.image as image7_0_
    from
        INF_PICTURE pictures0_
    where
        pictures0_.location_id_ref=?
10:08:15,937 DEBUG [LongType] binding '34' to parameter: 1
10:08:15,937 DEBUG [SQL]
    /* load collection be.ugent.dgfb.cab.model.infrastructure.Location.requests */ select
        requests0_.location_id_ref as location1_1_,
        requests0_.request_id_ref as request2_1_,
        request1_.id as id8_0_,
        request1_.version as version8_0_,
        request1_.description as descript3_8_0_,
        request1_.name as name8_0_
    from
        INF_LOCATION_REQUESTS requests0_
    left outer join
        INF_REQUEST request1_
            on requests0_.request_id_ref=request1_.id
    where
        requests0_.location_id_ref=?
10:08:15,937 DEBUG [LongType] binding '34' to parameter: 1
10:08:15,953 DEBUG [TestInfrastructureAdminServiceImpl] testBatch4() - end
10:08:15,953 DEBUG [TestInfrastructureAdminServiceImpl] tearDown() - start
10:08:15,953 INFO  [SpringUnitContext] ====== START TEARDOWN be.ugent.dgfb.cab.midtier.service.TestInfrastructureAdminServiceImpl ======
10:08:15,968 INFO  [SpringUnitContext] ====== END TEARDOWN be.ugent.dgfb.cab.midtier.service.TestInfrastructureAdminServiceImpl ======
10:08:15,968 DEBUG [TestInfrastructureAdminServiceImpl] tearDown() - end
[/code]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.