POJO类
package com.qiujy.domain;
import java.util.Set;
public class Student {
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<String> getHobbes() {
return hobbes;
}
public void setHobbes(Set<String> hobbes) {
this.hobbes = hobbes;
}
private Long id; //对像标识符OID
private String name; //姓名
private Set <String>hobbes; //个人爱好
public Student(){} //无参数构造方法
}
配置文件
<?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>
<class name="com.qiujy.domain.Student" table="student">
<id name="id" column="id" type="long">
<generator class="native" />
</id>
<property name="name" not-null="true" />
<!--
set元素用来映射java.util.Set类型的属性
name属性:指定要映射的属性名
table属性:指定对应的数据库表名
-->
<set name="hobbes" table="student_hobby">
<!-- key子元素:指定集合属性对应的表的外键列 -->
<key column="student_id" />
<!-- element子元素:映射集合内的元素 -->
<element type="string" column="hobby_name" not-null="true" />
</set>
</class>
</hibernate-mapping>
测试页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.main.testStudent" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
testStudent stu=new testStudent();
stu.getStudent();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'student.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
</body>
</html>