Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: All entities loaded when show method called for just one

  1. #1
    Join Date
    Oct 2010
    Posts
    29

    Default All entities loaded when show method called for just one

    Hi All,

    This is rather odd, when calling planets/1 to show the first planet entry things have been running incredibly slow. I set the log level to debug and I get the following:

    Code:
    DEBUG org.hibernate.loader.Loader - result row: EntityKey[uk.co.g4me.the_ancients.model.Planet#483637]
    DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Processing injected method of bean 'uk.co.g4me.the_ancients.model.Planet': PersistenceElement for transient javax.persistence.EntityManager uk.co.g4me.the_ancients.model.Planet.entityManager
    DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'galaxyEntityManagerFactory'
    There are lines like this for every Planet entity that is in the database, why are all these being injected when all I want to access is one planet.

    I'm rather confused. Any light the community could shed would be highly appreciated.

  2. #2
    Join Date
    May 2006
    Location
    Madrid
    Posts
    382

    Default

    Planets?

    Maybe you have some relationship (_ToOne, _ToMany) that eagerly needs the planets. The underlying JPA implementation (hibernate, for instance) will select all the necessary entities. And then, Spring will inject them the EntityManager (as they're @Configurables)

  3. #3
    Join Date
    Oct 2010
    Posts
    29

    Default

    Yes, Planets

    My initial thought was that these items were being Eagerly loaded. But:

    1) I am only requesting a load of a single Planet, no Planets reference each other.
    2) Each reference to a planet is Lazy loaded using @OneToMany(fetch = FetchType.Lazy)

    Perhaps I am setting this incorrectly?

    Is there a way to set Lazy loading globally? I'm using Hibernate.

  4. #4
    Join Date
    Oct 2010
    Posts
    29

    Default

    Ooops this post was duplicated
    Last edited by ArjunSol; Nov 25th, 2011 at 06:25 AM. Reason: DUPLICATE

  5. #5
    Join Date
    Oct 2010
    Posts
    29

    Default

    From what I can see the injection of these objects happens before the show method is even invoked.

    Could this be related to my explicitly setting classes in my persistence unit?

    Code:
    <persistence-unit name="galaxy" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <class>uk.co.g4me.the_ancients.model.Galaxy</class>
            <class>uk.co.g4me.the_ancients.model.Planet</class>
            <class>uk.co.g4me.the_ancients.model.PlanetClass</class>
            <class>uk.co.g4me.the_ancients.model.Sector</class>
            <class>uk.co.g4me.the_ancients.model.Star</class>
            <class>uk.co.g4me.the_ancients.model.StarClass</class>
            <class>uk.co.g4me.the_ancients.model.StarSystem</class>
            <exclude-unlisted-classes>true</exclude-unlisted-classes>
            <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
                <!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
                <property name="hibernate.hbm2ddl.auto" value="create"/>
                <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
                <property name="hibernate.connection.charSet" value="UTF-8"/>
            </properties>
        </persistence-unit>

  6. #6
    Join Date
    May 2006
    Location
    Madrid
    Posts
    382

    Default

    It's possible.

    You know that there's no need to list the classes, don't you? The classes have an static EntityManager autowired and so do the objects, due to the @Configurable annotation.

  7. #7
    Join Date
    Oct 2010
    Posts
    29

    Default

    Quote Originally Posted by jbbarquero View Post
    ...You know that there's no need to list the classes, don't you? The classes have an static EntityManager autowired and so do the objects, due to the @Configurable annotation.
    Indeed, however I have two databases one user and one game and require two persistenceUnits.

  8. #8
    Join Date
    May 2006
    Location
    Madrid
    Posts
    382

    Default

    Quote Originally Posted by ArjunSol View Post
    Indeed, however I have two databases one user and one game and require two persistenceUnits.
    Did you try...?

    Code:
    persistence setup ... --persistenceUnit db1
    entity ... --persistenceUnit db1
    ...
    persistence setup ... --persistenceUnit db2
    entity ... --persistenceUnit db2
    I mean, the options for configuring more than one database with Roo.

  9. #9
    Join Date
    Oct 2010
    Posts
    29

    Default

    Yes, all entities are configured correctly or their respective persistence units. I'm going to remove my dual setup and test to dismiss this as the cause.

  10. #10
    Join Date
    Oct 2010
    Posts
    29

    Default

    OK, so I thought I'd simplify this and ran the petclinic sample. After adding a single Owner and three Pets the same issue exists when calling /pets/1


    Code:
    DEBUG org.apache.tiles.impl.BasicTilesContainer - Render request recieved for definition 'pets/show'
    DEBUG org.hibernate.loader.Loader - loading collection: [com.springsource.petclinic.domain.Owner.pets#1]
    DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
    DEBUG org.hibernate.jdbc.ConnectionManager - opening JDBC connection
    DEBUG org.hibernate.SQL - select pets0_.owner as owner1_1_, pets0_.id as id1_, pets0_.id as id2_0_, pets0_.name as name2_0_, pets0_.owner as owner2_0_, pets0_.send_reminders as send3_2_0_, pets0_.type as type2_0_, pets0_.version as version2_0_, pets0_.weight as weight2_0_ from pet pets0_ where pets0_.owner=?
    DEBUG org.hibernate.jdbc.AbstractBatcher - about to open ResultSet (open ResultSets: 0, globally: 0)
    DEBUG org.hibernate.loader.Loader - result set contains (possibly empty) collection: [com.springsource.petclinic.domain.Owner.pets#1]
    DEBUG org.hibernate.loader.Loader - result row: EntityKey[com.springsource.petclinic.domain.Pet#1]
    DEBUG org.hibernate.loader.Loader - found row of collection: [com.springsource.petclinic.domain.Owner.pets#1]
    DEBUG org.hibernate.loader.Loader - result row: EntityKey[com.springsource.petclinic.domain.Pet#2]
    DEBUG org.hibernate.loader.Loader - found row of collection: [com.springsource.petclinic.domain.Owner.pets#1]
    DEBUG org.hibernate.loader.Loader - result row: EntityKey[com.springsource.petclinic.domain.Pet#3]
    DEBUG org.hibernate.loader.Loader - found row of collection: [com.springsource.petclinic.domain.Owner.pets#1]
    This is fine if you only have three pets, but I have hundreds of thousands of planets. Colour me confused.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •