Can Hibernate be used in performance sensitive applications?

后端 未结 4 669
滥情空心
滥情空心 2020-12-23 18:00

I\'m seeing performance problems with retrieving multiple instances of objects that have many relationships with other objects. I\'m using Spring and Hibernate\'s JPA implem

相关标签:
4条回答
  • 2020-12-23 18:07

    Besides "fetch" strategy you might also try setting batch fetch size in hibernate properties, so it will run joining queries not one by one but in batches.

    In your appContext.xml:

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        ...    
        <property name="hibernateProperties">
            <props>        
                ...
                <prop key="hibernate.default_batch_fetch_size">32</prop>
            </props>
        </property>
    </bean>
    

    So instead of:

    SELECT ... FROM Hobby WHERE personId=1
    SELECT ... FROM Hobby WHERE personId=2
    

    You will get:

    SELECT ... FROM Hobby WHERE personId in (1,2,...,32);
    SELECT ... FROM Hobby WHERE personId in (33,34,...,64);
    
    0 讨论(0)
  • 2020-12-23 18:10

    See my answer to your other question, if you read the whole of the FAQ you linked to:

    Follow the best practices guide! Ensure that all and mappings specify lazy="true" in Hibernate2 (this is the new default in Hibernate3). Use HQL LEFT JOIN FETCH to specify which associations you need to be retrieved in the initial SQL SELECT.

    A second way to avoid the n+1 selects problem is to use fetch="subselect" in Hibernate3.

    If you are still unsure, refer to the Hibernate documentation and Hibernate in Action.

    See the tips on improving performance. If you are not careful with joins, you will end up with Cartesian Product problems.

    0 讨论(0)
  • 2020-12-23 18:19

    Have You tried the "join" fetch strategy for the collections?

    0 讨论(0)
  • 2020-12-23 18:19

    If you need a feature of Hibernate and this feature is buggy you have two options: a) Submit a bugrequest and use a workaround (slow performance or handwritten sql) until the bug is fixed which will take a while b) Submit a bugrequest along with a bugfix and tests. (of course you could just use the bugfix and skip the bugrequest and test part).

    0 讨论(0)
提交回复
热议问题