使用了hibernate进行关系映射的时候,比如有一对多的关系,一个测试项目对应多个测试用例模块,部分的关系映射表:
代码
<
many-to-one
name
="testProject"
class
="com.eyely.pojos.TestProject"
>
< column name ="f_test_project_id" sql-type ="nvarchar2(32)" />
</ many-to-one >
< set name ="testCaseModules" lazy ="true" inverse ="false" >
< key >
< column name ="f_test_project_id" sql-type ="nvarchar2(32)" />
</ key >
< one-to-many class ="com.eyely.pojos.TestCaseModule" />
</ set >
< column name ="f_test_project_id" sql-type ="nvarchar2(32)" />
</ many-to-one >
< set name ="testCaseModules" lazy ="true" inverse ="false" >
< key >
< column name ="f_test_project_id" sql-type ="nvarchar2(32)" />
</ key >
< one-to-many class ="com.eyely.pojos.TestCaseModule" />
</ set >
相关Java文件:
代码
//
相关属性
private String name;
private String description;
private TestCaseModule parent;
private TestProject testProject;
/ ** 不属于同一Java文件,为了方便,写在一起了 */
testCaseModule.setDescription(cmd.getParameter( " description " ));
testCaseModule.setName(cmd.getParameter( " name " ));
testCaseModule.setParent((TestCaseModule)session.get(TestCaseModule. class , cmd.getParameter( " parent " )));
testCaseModule.setTestProject((TestProject)session.get(TestProject. class , cmd.getParameter( " testProject " )));
private String name;
private String description;
private TestCaseModule parent;
private TestProject testProject;
/ ** 不属于同一Java文件,为了方便,写在一起了 */
testCaseModule.setDescription(cmd.getParameter( " description " ));
testCaseModule.setName(cmd.getParameter( " name " ));
testCaseModule.setParent((TestCaseModule)session.get(TestCaseModule. class , cmd.getParameter( " parent " )));
testCaseModule.setTestProject((TestProject)session.get(TestProject. class , cmd.getParameter( " testProject " )));
添加操作:*.jsp
代码
//
只贴部分代码,代码部分已经封装好,这里只供参考
TestCaseModuleProcessor processor = server.getProcessor(TestCaseModuleProcessor. class );
Command cmd = new HttpServletCommand(request, response);
// TODO: miss validate.
processor.create(cmd);
TestCaseModuleProcessor processor = server.getProcessor(TestCaseModuleProcessor. class );
Command cmd = new HttpServletCommand(request, response);
// TODO: miss validate.
processor.create(cmd);
如果访问*.jsp文件时,因为进行添加时,首先会先查找关联关系中的测试项目的Id ,获取到之后再进行添加的操作,如果获取不到测试项目的id,那么将会跑出一个异常,如大概是这样的信息:
java.lang.IllegalArgumentException: id to load is required for loading
org.hibernate.event.LoadEvent.<init>(LoadEvent.java:51)
org.hibernate.event.LoadEvent.<init>(LoadEvent.java:33)
org.hibernate.impl.SessionImpl.get(SessionImpl.java:812)
org.hibernate.impl.SessionImpl.get(SessionImpl.java:808)
com.zyeeda.projs.cmsz.processors.TestCaseModuleProcessor.create(TestCaseModuleProcessor.java:56)
org.apache.jsp.test_005fcase_005fmodule.create_jsp._jspService(create_jsp.java:81)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
那么如果要测试你的jsp文件是否正确的话,可以传递一个参数来测试。如在URL传递一个参数来测试:你的JSP页面的路径?testProject=1,这样就不会再
出现
id to load is required for loading这样的异常了,如果你的jsp或者其他代码出现错误的话,浏览器将会提示其他的错误信息,这时你就可以调试你的代码了