Hibernate中,One2Many和Many2One的双向映射关系的XML实现方式

1、在Hibernate中,One2Many和Many2One的双向映射关系的XML实现方式

2、Student.java的代码

public class Student {
	private int id;
	private int age;
	private String name;

	// 持有多的一方为外键
	private ClassRoom classRoom;

	// set get
}

3、Student.hbm.xml的配置文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 配置实体类,所在的package的名称 -->
<hibernate-mapping package="com.hibernate.entity">
	<class name="Student" table="student">
		<!-- 主键 ,映射 -->
		<id name="id" column="id">
			<!-- 自动生成主键的规则 -->
			<generator class="native" />
		</id>
		<!-- 非主键,映射 -->
		<property name="name" column="name"></property>
		<property name="age" column="age"></property>
		<!--增加多对一的映射 -->
		<!--并制定外键名称 -->
		<many-to-one name="classRoom" column="classid"></many-to-one>
	</class>
</hibernate-mapping>

4、ClassRoom.java的代码

public class ClassRoom {
	private int id;
	private String classRoomName;
	// 持有多的一方
	private Set<Student> stus = new HashSet<Student>();

	// get set
}

5、ClassRoom.hbm.xml的配置文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 配置实体类,所在的package的名称 -->
<hibernate-mapping package="com.hibernate.entity">
	<class name="ClassRoom" table="classroom">
		<!-- 主键 ,映射 -->
		<id name="id" column="id">
			<!-- 自动生成主键的规则 -->
			<generator class="native" />
		</id>
		<!-- 非主键,映射 -->
		<property name="classRoomName" column="classRoomName"></property>
		<!--one2Many的配置 -->
		<set name="stus" inverse="true" lazy="false">
			<!-- key 用来指定对外的外键名称 -->
			<key column="classid" />
			<!-- set集合里面,包含的数据类型 -->
			<one-to-many class="com.hibernate.entity.Student" />
		</set>
	</class>
</hibernate-mapping>

6、hibernate.cfg.xml的配置

6.1  将Student.hbm.xml和ClassRoom.hbm.xml,加入到hibernate.cfg.xml的配置文件里面

<!--3.加载实体的映射文件 -->
		<mapping resource="com/hibernate/entity/Student.hbm.xml" />
		<mapping resource="com/hibernate/entity/ClassRoom.hbm.xml" />

6.2  hibernate.cfg.xml的配置的完整内容

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<!-- 通常,一个session-factory节点代表一个数据库 -->
	<session-factory>

		<!-- 1. 数据库连接配置 -->
		<property name="hibernate.connection.driver_class">
			com.mysql.jdbc.Driver
		</property>
		<property name="hibernate.connection.url">
			jdbc:mysql:///hibernate
		</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">1234</property>
		<!-- 数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql -->
		<property name="hibernate.dialect">
			org.hibernate.dialect.MySQL5Dialect
		</property>


		<!-- 2. 其他相关配置 -->
		<!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
		<property name="hibernate.show_sql">true</property>
		<!-- 2.2 格式化sql -->
		<property name="hibernate.format_sql">true</property>
		<!-- 2.3 自动建表 -->
		<property name="hibernate.hbm2ddl.auto">update</property>

		<!--3.加载实体的映射文件 -->
		<mapping resource="com/hibernate/entity/Student.hbm.xml" />
		<mapping resource="com/hibernate/entity/ClassRoom.hbm.xml" />
	</session-factory>
</hibernate-configuration>


7、测试代码

public class HibernateTest {

	@Test
	public void testone2many() {

		Student stu1 = new Student();
		stu1.setAge(18);
		stu1.setName("18");

		Student stu2 = new Student();
		stu2.setAge(19);
		stu2.setName("19");

		ClassRoom classRoom = new ClassRoom();
		classRoom.setClassRoomName("计算机技术");
		// 增加ClassRoom和Student的注册关系
		// classRoom.getStus().add(stu1);
		// classRoom.getStus().add(stu2);
		// 在双向关联的映射中,上面的不要写。就可以实现了
		// 增加Student和ClassRoom的注册关系
		stu1.setClassRoom(classRoom);
		stu2.setClassRoom(classRoom);
		// 保存到数据库
		IClassRoomDao roomDao = new ClassRoomDaoImpl();
		IStudentDao studentDao = new StudentDaoImpl();
		// 在保存一的一方
		roomDao.add(classRoom);
		// 先保存多的一方
		studentDao.add(stu1);
		studentDao.add(stu2);

	}

	@Test
	public void test2() {
		IClassRoomDao roomDao = new ClassRoomDaoImpl();
		Set<Student> stus = roomDao.load(1).getStus();
		System.out.println(stus);
	}

	@Test
	public void test3() {
		IStudentDao studentDao = new StudentDaoImpl();
		ClassRoom classRoom = studentDao.load(1).getClassRoom();
		System.out.println(classRoom);
	}

}

8、代码下载

代码下载


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值