前天晚上写的测试类的代码,暂且不用去看他。
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.hb.petstore.struts.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.struts.ActionSupport;
import com.hb.petstore.service.UserService;
import com.hb.petstore.vo.User;
public class FindUserAction extends Action {
private UserService userService;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
try {
List<User> users=userService.findAllUser();
request.getSession().setAttribute("users", users);
} catch (Exception e) {
e.printStackTrace();
}
return new ActionForward("/index.jsp");
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
}
这个类中的插入数据的思想是,与order表有外键关联的表,如item表,在service层都看作对象,Item。这些service中的对象的属性,就是原来表中的列。 所以插入一个完整的订单对象后,会把与订单表级联的所有表都插满数据。
hibernate的好处,就是所有的关系,都在配置文件中显示,同样修改配置文件,就能级联数据库里的操作。
我遇到的问题是,自动生成的配置文件中,总没有级联属性,而现有的学习资料里面没有提到,对cascade这一属性的配置。
以致还跑去和组长讨论表的关联合理性问题。
贴下配置文件中的内容:
<?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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.hb.petstore.vo.Order" table="T_ORDER" schema="SCOTT">
<id name="oid" type="java.lang.Long">
<column name="OID" precision="22" scale="0" />
<generator class="increment" />
</id>
<many-to-one name="customer" class="com.hb.petstore.vo.Customer" fetch="select" cascade="save-update">
<column name="CID" precision="22" scale="0" />
</many-to-one>
<property name="ordNo" type="java.lang.String">
<column name="ORD_NO" length="20" />
</property>
<property name="orderDate" type="java.util.Date">
<column name="ORDER_DATE" length="7" />
</property>
<property name="orderState" type="java.lang.String">
<column name="ORDER_STATE" length="20" />
</property>
<set name="OItems" inverse="true" cascade="save-update">
<key>
<column name="OID" precision="22" scale="0" />
</key>
<one-to-many class="com.hb.petstore.vo.Item" />
</set>
</class>
</hibernate-mapping>
桔黄色的便是可恶的cascade属性...