`
Wind_ZhongGang
  • 浏览: 259715 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Hibernate one-to-many 多方单向连接表关联

阅读更多

 

 

  Hibernate中持久化实体间一对多关联,具体关联关系为多方,单向,连接表关联。

 

  一。Husband

 

package com.dream.model.couple;

import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 11-10-15
 * Time: 下午3:53
 */
public class Husband {
    private Integer id;
    private String name;

    public Husband(String name) {
        this.name = name;
    }

    public Husband() {
    }
}

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping default-access="field">

    <class name="com.dream.model.couple.Husband" table="husband" dynamic-insert="true" dynamic-update="true">
        <id name="id" column="id" type="java.lang.Integer">
            <generator class="native"/>
        </id>
        <property name="name" column="name" type="java.lang.String"/>
    </class>

</hibernate-mapping>

 

  二。Wife

 

package com.dream.model.couple;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 11-10-15
 * Time: 下午3:52
 */
public class Wife {
    private Integer id;
    private String name;

    private Husband husband;

    public Wife(String name) {
        this.name = name;
    }

    public Wife() {
    }

    public Wife(String name, Husband husband) {
        this.name = name;
        this.husband = husband;
    }

    public String name() {
        return this.name;
    }
}
 

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping default-access="field">

    <class name="com.dream.model.couple.Wife" table="wife" dynamic-insert="true" dynamic-update="true">
        <id name="id" column="id" type="java.lang.Integer">
            <generator class="native"/>
        </id>
        <property name="name" column="name" type="java.lang.String"/>

        <join table="couple">
            <key column="wifeid"/>
            <many-to-one name="husband" column="husbandid" class="com.dream.model.couple.Husband"/>
        </join>
    </class>

</hibernate-mapping>

 

  三。CoupleDao

 

 

package com.dream.dao.standard;

import com.dream.model.couple.Husband;
import com.dream.model.couple.Wife;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 11-10-15
 * Time: 下午4:24
 */
public interface CoupleDao {

    void saveOrUpdateHusband(Husband husband);

    List<Husband> findHusbandsByName(String name);

    void saveOrUpdateWife(Wife wife);
}

 

package com.dream.dao;

import com.dream.dao.standard.CoupleDao;
import com.dream.model.couple.Husband;
import com.dream.model.couple.Wife;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 11-10-15
 * Time: 下午4:24
 */
public class CoupleDaoImpl extends HibernateDaoSupport implements CoupleDao {

    @Override
    public void saveOrUpdateHusband(Husband husband) {
        getHibernateTemplate().saveOrUpdate(husband);
    }

    @Override
    public List<Husband> findHusbandsByName(String name) {
        return getHibernateTemplate().find("from Husband husband where husband.name=?", name);
    }

    @Override
    public void saveOrUpdateWife(Wife wife) {
        getHibernateTemplate().saveOrUpdate(wife);
    }
}

 

  四。CoupleService

 

 

package com.dream.service.standard;

import com.dream.model.couple.Husband;
import com.dream.model.couple.Wife;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 11-10-15
 * Time: 下午4:23
 */
public interface CoupleService {

    void saveOrUpdateHusband(Husband husband);

    List<Husband> loadHusbandsByName(String name);

    void saveOrUpdateWife(Wife wife);
}

 

package com.dream.service;

import com.dream.dao.standard.CoupleDao;
import com.dream.model.couple.Husband;
import com.dream.model.couple.Wife;
import com.dream.service.standard.CoupleService;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 11-10-15
 * Time: 下午4:23
 */
public class CoupleServiceImpl implements CoupleService {

    private CoupleDao coupleDao;

    @Override
    public void saveOrUpdateHusband(Husband husband) {
        coupleDao.saveOrUpdateHusband(husband);
    }

    @Override
    public List<Husband> loadHusbandsByName(String name) {
        return coupleDao.findHusbandsByName(name);
    }

    @Override
    public void saveOrUpdateWife(Wife wife) {
        coupleDao.saveOrUpdateWife(wife);
    }

    public void setCoupleDao(CoupleDao coupleDao) {
        this.coupleDao = coupleDao;
    }

}

 

  五。testDB

 

 

db.url=jdbc:mysql://localhost:3306/couple?createDatabaseIfNotExist=true&amp;useUnicode=true&amp;characterEncoding=utf-8

db.driver=com.mysql.jdbc.Driver

db.username=root

db.password=root

hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

hibernate.show_sql=true

hibernate.hbm2ddl.auto=update

hibernate.jdbc.batch_size=100

 

  六。testDataSource

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
       default-autowire="byName">

    <context:property-placeholder location="classpath:testDB.properties"/>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="${db.driver}"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingLocations">
            <list>
                <value>classpath:/hibernate_mappings/Husband.hbm.xml</value>
                <value>classpath:/hibernate_mappings/Wife.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="service" expression="execution(* com.dream.service..*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="service"/>
    </aop:config>

    <bean id="coupleDao" class="com.dream.dao.CoupleDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="coupleService" class="com.dream.service.CoupleServiceImpl">
        <property name="coupleDao" ref="coupleDao"/>
    </bean>
</beans>

 

   七。Test

 

package com.dream.couple;

import com.dream.model.couple.Husband;
import com.dream.model.couple.Wife;
import com.dream.service.standard.CoupleService;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 11-10-15
 * Time: 下午4:10
 */
public class HibernateOneToManyJoinTest extends TestCase {
    private CoupleService coupleService;

    @Override
    public void setUp() throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:testDataSource.xml");
        coupleService = (CoupleService) context.getBean("coupleService");
    }

    public void testOneToOneJoin() throws Exception {

        Husband husband = new Husband("husband");
        coupleService.saveOrUpdateHusband(husband);

        Wife wife1 = new Wife("wife1", husband);
        coupleService.saveOrUpdateWife(wife1);

        Wife wife2 = new Wife("wife2", husband);
        coupleService.saveOrUpdateWife(wife2);

        Wife wife3 = new Wife("wife3", husband);
        coupleService.saveOrUpdateWife(wife3);
    }
}

 

  跑完测试,发出的sql语句

 

  result

 

  来看看跑完测试后,数据库中相关的数据表及数据:

 

  result1

 

  result2

 

  • 大小: 13.7 KB
  • 大小: 17.2 KB
  • 大小: 8.4 KB
0
0
分享到:
评论

相关推荐

    HibernateAPI中文版.chm

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. ...

    hibernate3.2中文文档(chm格式)

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. ...

    Hibernate+中文文档

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. ...

    hibernate学习笔记

    hibernate多对一关联映射(Hibernate_Many2One) 7 hibernate一对一主键关联映射(单向关联Person----&gt;IdCard) 8 hibernate一对一主键关联映射(双向关联Person&lt;----&gt;IdCard) 9 hibernate一对一唯一外键关联映射...

    Hibernate中文详细学习文档

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. ...

    Hibernate_Annotation关联映射

    多对一(Many-to-One) 使用@ManyToOne批注来实现多对一关联。 @ManyToOne批注有一个名为targetEntity的参数,该参数定义了目标实体名,通常不需要定义该参数,因为在大部分情况下默认值(表示关联关系的属性类型)就...

    Hibernate 中文 html 帮助文档

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. 双向...

    最全Hibernate 参考文档

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. 双向...

    hibernate 体系结构与配置 参考文档(html)

    多对一(many-to-one) 5.1.11. 一对一 5.1.12. 自然ID(natural-id) 5.1.13. 组件(component), 动态组件(dynamic-component) 5.1.14. properties 5.1.15. 子类(subclass) 5.1.16. 连接的子类(joined-subclass...

    Hibernate教程

    8.3. 使用连接表的单向关联(Unidirectional associations with join tables) 8.3.1. 一对多(one to many) 8.3.2. 多对一(many to one) 8.3.3. 一对一(one to one) 8.3.4. 多对多(many to many) 8.4. ...

    Hibernate3的帮助文档

    6.1.10. 多对一(many-to-one) 6.1.11. 一对一 6.1.12. 组件(component), 动态组件(dynamic-component) 6.1.13. properties 6.1.14. 子类(subclass) 6.1.15. 连接的子类(joined-subclass) 6.1.16. 联合子类...

    NHibernate中文文档

    使用表连接的单向关联 16 双向关联 16 使用表连接的双向关联 16 第7章示例: Parent/Child 16 关于collections 17 双向的一对多关系(Bidirectional one-to-many) 17 级联生命周期(Cascading lifecycle) 18 级联更新...

    hibernate 框架详解

    多对一(many-to-one) 6.1.11. 一对一 6.1.12. 组件(component), 动态组件(dynamic-component) 6.1.13. properties 6.1.14. 子类(subclass) 6.1.15. 连接的子类(joined-subclass) 6.1.16. 联合子类(union-...

    hibernate3.04中文文档.chm

    8.3. 使用连接表的单向关联(Unidirectional associations with join tables) 8.3.1. 一对多(one to many) 8.3.2. 多对一(many to one) 8.3.3. 一对一(one to one) 8.3.4. 多对多(many to many) 8.4. ...

    Hibernate3+中文参考文档

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. 双向...

    Hibernate参考文档

    5.1.10. 多对一(many-to-one) 5.1.11. 一对一 5.1.12. 自然ID(natural-id) 5.1.13. 组件(component), 动态组件(dynamic-component) 5.1.14. properties 5.1.15. 子类(subclass) 5.1.16. 连接的子类(joined-...

    hibernate_reference中文文档.pdf

    1.2.2. 单向 Set-based 的关联 ......................................... 14 1.2.3. 使关联工作 ..................................................... 15 1.2.4. 值类型的集合 ..................................

    jdbc基础和参考

    many-to-one:标签中对于cascade的取值delete,delete-orphan,all-delete-orphan(只用unique属性值不为true不能出现)慎用 cascade:级联属性 none:不做任何级联操作 save-update:对当前对象执行save,update, ...

    Spring面试题

    类与类之间的关系主要体现在表与表之间的关系进行操作,它们都市对对象进行操作,我们程序中把所有的表与类都映射在一起,它们通过配置文件中的many-to-one、one-to-many、many-to-many、 4. 说下Hibernate的缓存...

Global site tag (gtag.js) - Google Analytics