0

How to get only parent without children in Hibernate and without lazy exception, when you trying to access chidlren filed after closing session?
I mean, a kind of pure parent obtainingm but I beed to be sure, that somebody will not obtain them with getter or will not catch a lazy exception.

    <class name="com.electronic.commerce.models.Category" table="Category">
    <id name="id" type="long" access="property">
        <column name="categoryId" length="20"/>
        <generator class="native"/>
    </id>
    <property name="name" column="categoryName" type="string"/>
    <property name="parentId" column="categoryParentId" type="long"/>
    <bag name="children" table="Category" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="categoryParentId" not-null="true"/>
        </key>
        <one-to-many class="com.electronic.commerce.models.Category"/>
    </bag>
</class>
  • 1
    check http://stackoverflow.com/questions/25362831/solve-hibernate-lazy-init-issue-with-hibernate-enable-lazy-load-no-trans link here for answer – Amit Naik Mar 12 '16 at 16:47

1 Answers1

0

Mixed solution
Xml:

<prop key="hibernate.enable_lazy_load_no_trans">false</prop>

Java:

DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition();
defaultTransactionDefinition.setPropagationBehavior(transactionDefinition);
transactionManager.getTransaction(defaultTransactionDefinition);