Room表和worker表
Room映射文件如下:
<hibernate-mapping>
<class name="com.data.Room" table="room" schema="dbo" catalog="xmgl" dynamic-update="true" dynamic-insert="true">
<id name="roomId" type="java.lang.Integer">
<column name="room_id" />
<generator class="identity" />
</id>
<property name="roomName" type="java.lang.String">
<column name="room_name" length="10" />
</property>
<set name="workers" inverse="true" cascade="all">
<key>
<column name="worker_roomid" />
</key>
<one-to-many class="com.data.Worker" />
</set>
</class>
</hibernate-mapping>
Worker映射文件如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.data.Worker" table="worker" schema="dbo" catalog="xmgl" >
<id name="workerId" type="java.lang.Integer">
<column name="worker_id" />
<generator class="identity" />
</id>
<many-to-one name="room" class="com.data.Room" fetch="select" update="true" insert="true">
<column name="worker_roomid" />
</many-to-one>
<property name="workerName" type="java.lang.String">
<column name="worker_name" length="10" />
</property>
<property name="workerPassword" type="java.lang.String">
<column name="worker_password" length="10" />
</property>
<property name="workerSex" type="java.lang.String">
<column name="worker_sex" length="4" />
</property>
<property name="workerAge" type="java.lang.Integer">
<column name="worker_age" />
</property>
<property name="workerXueli" type="java.lang.String">
<column name="worker_xueli" length="10" />
</property>
<set name="tasks" inverse="true" cascade="all">
<key>
<column name="worker_id" />
</key>
<one-to-many class="com.data.Task" />
</set>
</class>
</hibernate-mapping>
Workeradd:完成插入
package com.worker;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.data.HibernateSessionFactory;
import com.data.Room;
import com.data.Worker;
public class Workeradd extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String name = request.getParameter("name").trim();
String password = request.getParameter("password").trim();
String sex = request.getParameter("sex").trim();
int age = Integer.valueOf(request.getParameter("age").trim());
Integer roomid = Integer.valueOf(request.getParameter("roomid").trim()); String xueli = request.getParameter("xueli").trim();
Session session =HibernateSessionFactory.getSession();
Transaction tx=session.beginTransaction();
Room room=(Room)session.get(Room.class, roomid);
Worker worker=new Worker();
worker.setWorkerName(name);
worker.setWorkerAge(age);
worker.setWorkerPassword(password);
worker.setWorkerSex(sex);
worker.setWorkerXueli(xueli);
worker.setRoom(room);
room.getWorkers().add(worker);
session.save(room);
tx.commit();
session.close();
response.sendRedirect("../workermanage/addworker.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
前台jsp:
<%@ page language="java" contentType="text/html; charset=GB2312" %>
<html>
<head>
<script language="javascript">
function check(){
if(document.workeradd.id.value.length==0 || document.workeradd.password.value.length==0 ){
alert("员工编号或密码不能为空");
return false;
}
return true;
}
</script>
</head>
<body bgcolor="#f0f8ff">
<form name="workeradd" method="post" action="../servlet/Workeradd" onsubmit="return check()">
<center>
<table bgcolor="#f0f8ff" width="650">
<tr><td>员工姓名</td><td><input type="text" name="name"></td></tr>
<tr><td> </td><td> </td></tr>
<tr><td>员工密码</td><td><input type="text" name="password"></td></tr>
<tr><td> </td><td> </td></tr>
<tr>
<td>员工性别</td>
<td><select name="sex" >
<option>boy</option>
<option>girl</option>
</select>
</td>
</tr>
<tr><td> </td><td> </td></tr>
<tr><td>年    龄</td><td><input type="text" name="age"></td></tr>
<tr><td> </td><td> </td></tr>
<tr><td>工作室id</td><td><input type="text" name="roomid"></td></tr>
<tr><td> </td><td> </td></tr>
<tr>
<td>学历</td>
<td><select name="xueli" >
<option>中专</option>
<option>大专</option>
<option>本科</option>
<option>硕士</option>
</select>
</td>
</tr>
<tr><td> </td><td> </td></tr>
<tr><td> </td><td> </td></tr>
<tr><td><input type="submit" value="添加"></td><td><input type="reset" value="取消"></td></tr>
</table>
</center>
</form>
</body>
</html>
posted on 2007-07-16 12:32
lcgxc 阅读(545)
评论(0) 编辑 收藏 所属分类:
hibernate