Results 1 to 10 of 16

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

Hybrid View

  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.

Posting Permissions

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