`

Hibenrate之多对多关联

阅读更多

Hibernate多对多关联可以分成单向和双向,多对多关系会借助中间表来保存映射信息,所以一般少用;

  • 单向

实例:学生可以在多个学校就读过,多个学校也会有多个学生;

TStudent:

public class TStudent implements java.io.Serializable {
	private static final long serialVersionUID = 1L;

	private Integer stuId;
	private String name;
	private Set<TSchool> tschool;
        get()/set()

 TStudent.hbm.xml:

<hibernate-mapping package="com.keith.many2many">
	<class name="TStudent" table="TStudent">
		<id name="stuId">
			<generator class="native"/>
		</id>
	
		<property name="name" />
		
		<set name="tschool" table="stu_school_link" cascade="all">
			<key column="stuId"/>
			<many-to-many class="TSchool" column="schoolId"/>
		</set>
	</class>	
</hibernate-mapping>

 TSchool:

public class TSchool implements java.io.Serializable {
	private static final long serialVersionUID = 1L;

	private Integer schoolId;
	private String name;
        get()/set()
}

 TSchool.hbm.xml:

<hibernate-mapping package="com.keith.many2many">
	<class name="TSchool" table="TSchool">
		<id name="schoolId">
			<generator class="native"/>
		</id>
		<property name="name" />
	</class>	
</hibernate-mapping>

 测试:

//添加数据
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		TStudent student = new TStudent();
		student.setName("柯");

		Set<TSchool> schools = new HashSet<TSchool>();
		TSchool school = new TSchool();
		school.setName("小学");
		TSchool school1 = new TSchool();
		school1.setName("中学");
		TSchool school2 = new TSchool();
		school2.setName("大学");

		schools.add(school);
		schools.add(school1);
		schools.add(school2);
		student.setTschool(schools);
		
		session.save(student);
		session.getTransaction().commit();

//查询数据
		TStudent stu = (TStudent) session.load(TStudent.class, 1);
		System.out.println("student's Name:" + stu.getName());

		Iterator<TSchool> it = stu.getTschool().iterator();
		TSchool school = null;
		while (it.hasNext()) {
			school = it.next();
			System.out.println("student's school Name"+school.getName());
		}
  •  双向,在TSchool一方加入TStudent对象;

TSchool:

public class TSchool implements java.io.Serializable {
	private static final long serialVersionUID = 1L;

	private Integer schoolId;
	private String name;
	private Set<TStudent> stus = new HashSet();
        get()/set()
}

 TSchool.hbm.xml:

<hibernate-mapping package="com.keith.many2many.doubleSide">
	<class name="TSchool" table="TSchool">
		<id name="schoolId">
			<generator class="native"/>
		</id>
	
		<property name="name" />
		<set name="stus" table="stu_school_link" cascade="save-update">
			<key column="schoolId"/>
			<many-to-many column="stuId" class="TStudent"/>
		</set>
	</class>	
</hibernate-mapping>

 TStudent:

public class TStudent implements java.io.Serializable {
	private static final long serialVersionUID = 1L;

	private Integer stuId;
	private String name;
	private Set<TSchool> tschool;
        get()/set();
}
 

TStudent.hbm.xml:

<hibernate-mapping package="com.keith.many2many.doubleSide">
	<class name="TStudent" table="TStudent">
		<id name="stuId">
			<generator class="native"/>
		</id>
	
		<property name="name" />
		
		<set name="tschool" table="stu_school_link" cascade="save-update">
		<!-- "stuId":连接表中关联当前实体类的列名 -->
			<key column="stuId"/>
			<!-- "schoolId":是连接表中关联本实体类的外键 -->
			<many-to-many class="TSchool" column="schoolId"/>
		</set>
	</class>	
</hibernate-mapping>

 附上测试类;

 

0
0
分享到:
评论

相关推荐

    hibernatewuwei

    hibenrate的代码本人的培训的代码,老师讲解的很好。是一个学习hibenrate的好的工具

    hibernate struts2 spring

    3. Spring:Spring框架是Java企业级应用的核心框架,它覆盖了从依赖注入到事务管理、安全控制等多个方面。在持久层,Spring提供了JDBC抽象层和对ORM框架(如Hibernate)的支持。在服务层,Spring的IOC(Inversion of...

    Hibernate更新某些字段

    Hibernate 是一个基于 Java 的 ORM 框架,它提供了对数据库的操作接口。在实际开发中,我们经常需要更新数据库中的某些字段,而不是所有字段。那么,如何使用 Hibernate 实现更新某些字段呢? 首先,我们需要了解 ...

    图书馆之春:您可以在其中借书的图书馆Web应用程序。 这是Spring MVC和Hibernate项目

    数据库和配置MySQL 飞路(用于数据迁移) GradleTomcat吉特Google Book API 后端技术JavaSpring MVC,Spring AOP,Spring Security Hibernate ORM,Hibernate Validator,Hibenrate Search(Lucene) 翻新,JSON ...

    hibernate学习资料

    hibernahibernate学习资料te学习资料hibernate学习资料v

    公交车查询系统,公交车查询系统

    公交车查询系统是一款基于Java...总的来说,公交车查询系统是一个综合运用了Java、Spring、Hibernate和Web技术的项目,它通过高效的数据处理和友好的用户界面,实现了对公共交通信息的快速查询,为公众出行提供了便利。

Global site tag (gtag.js) - Google Analytics