简体   繁体   中英

How to get only parent without children in Hibernate and without lazy exception?

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>

Mixed solution
Xml:

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

Java:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM