简体   繁体   中英

hibernate and spring dao

I have two tables Parent and Child. Child table references to Parent table.

In application configuration of spring dao. Can't I give configuration of only child? like below.

<bean id="ChildDAOSpringTarget" class="project.dao.spring.ChildDAOSpring">
    <property name="sessionFactory">
        <ref local="sessionFactory" />
    </property>
</bean>

<bean id="ChildDAO"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager">
        <ref local="transactionManager" />
    </property>
    <property name="target">
        <ref local="ChildDAOSpringTarget" />
    </property>

    <property name="transactionAttributes">
        <props>
            <prop key="get*">PROPAGATION_REQUIRED,readOnly
            </prop>
            <prop key="find*">PROPAGATION_REQUIRED,readOnly
            </prop>
            <prop key="load*">PROPAGATION_REQUIRED,readOnly
            </prop>
            <prop key="store*">PROPAGATION_REQUIRED</prop>
            <prop key="add*">PROPAGATION_REQUIRED</prop>
        </props>
    </property>
</bean>

Do I have to have add this kind of configuration and classes for Parent even?

我没有在applicationContext.xml中配置父表和子表之间的关系,但在modelXXX.hbm.xml文件中设置了该关系,或者可以使用休眠工具生成模型链接

In hibernate, parent-child relationship has been defined in hbm file or it will be annotation based. And in application configuration file, you have to define database connection's properties, session factory, bean mapping etc.

Yep as mentioned above... Check here for one to many tutorial which is likely what you are trying to do. http://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example/ The child becomes a collection of the parent and is accessed as such. Spring doesn't need to reference the child - instead you reference the children through the parent like so: parent.getChildren().[...]

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