调查问卷~多对多关系~~在web开发

ABAP1314 2009-08-04 10:00:12
由于工作需要 网站有些市场调查问卷类型的数据 多个复选框 多对多关系的处理 由于自己没怎么接触过 所以不知道怎么处理~~~ 求高人指点······我现在的思路 只是 建立中间表 通过数组 获取数据 再 通过循环存储 数据 可我不知道这样对不对~~我也知道其中有很多 毛病 。。。。。。请指点



//填写调查问卷 获取前台表单数据
String name = this.getUsername(request, response);
User user = (User) userManager.getUserByName(name);
String[] health = request.getParameterValues("health");
String[] family = request.getParameterValues("family");
String[] hope = request.getParameterValues("hope");
System.out.println(user.getUserId());
if(memberManager.getUserToHealthByUserId(user.getUserId()).isEmpty()) {
for (int i = 0; i < health.length; i++) {
UserToHealth uh=new UserToHealth();
uh.setHeaId(new Integer(health[i]));
uh.setUserId(user.getUserId());

Long result=memberManager.addUserToHealth(uh);
if(result<=0)
{
String message="addUserToHealth is errer ";
model.put("message", message);
return new ModelAndView(errer,model);
}

}

}else
{
for (int i = 0; i < health.length; i++) {
UserToHealth uh=new UserToHealth();
uh.setHeaId(new Integer(health[i]));
uh.setUserId(user.getUserId());
memberManager.saveUserToHealth(uh);
}
}
return new ModelAndView(insert4,model);
}
...全文
166 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ABAP1314 2009-08-07
  • 打赏
  • 举报
回复
呵呵 还是依靠 hibernate 解决了
public ModelAndView doInsert3(HttpServletRequest request,
HttpServletResponse response, ControllerHelper helper, Map model)
throws Exception {
//填写调查问卷 获取前台表单数据
String name = this.getUsername(request, response);
User user = (User) userManager.getUserByName(name);
String[] health = request.getParameterValues("health");
String[] family = request.getParameterValues("family");
String[] hope = request.getParameterValues("hope");
Set heaSet = new HashSet();
Set hopSet = new HashSet();
Set famSet = new HashSet();
Buyer buyer =memberManager.getBuyerByUserId(user.getUserId());
for (int i = 0; i < health.length; i++) {
Health uh=memberManager.getHealthById(Integer.parseInt(health[i]));
heaSet.add(uh);
}
System.out.println("hop:");
for (int i = 0; i < hope.length; i++) {

Hope uh = memberManager.getHopeById(Integer.parseInt(hope[i]));
hopSet.add(uh);

}
for (int i = 0; i < family.length; i++) {
Family uf = memberManager.getFamilyById(Integer.parseInt(family[i]));
famSet.add(uf);

}

buyer.setHealth(heaSet);
buyer.setHope(hopSet);
buyer.setFamily(famSet);
buyer.setBuytype(1);
System.out.println(" all is ok" );
memberManager.saveBuyer(buyer);
System.out.println("*****健康困扰提交成功*****");
return new ModelAndView(filesuccess,model);
}


hbm.xml


<set name="health" table="souhu_usertohealth" cascade="save-update" lazy="false">
<key column="id"></key>
<many-to-many class="Health" column="heaId"></many-to-many>
</set>
<set name="family" table="souhu_usertofamily" cascade="save-update" lazy="false">
<key column="id"></key>
<many-to-many class="Family" column="famId"></many-to-many>
</set>
<set name="hope" table="souhu_usertohope" cascade="save-update" lazy="false">
<key column="id"></key>
<many-to-many class="Hope" column="hopId"></many-to-many>
</set>
youbin_ 2009-08-05
  • 打赏
  • 举报
回复
真不好意思,我把映射文件用javacode括起来后,就成了上面的效果了,右键看源代码可以看到。。。
浪费大家时间了
youbin_ 2009-08-05
  • 打赏
  • 举报
回复
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="com.excellence.components.survey.domain.SurveyResult" table="T_SURVEY_RESULT" lazy="false">

<id name="id" type="java.lang.Integer" column="ID" length="10">
<generator class="native"/>
</id>

<many-to-one
name="surveyItem"
column="SURVEY_ITEM_ID"
class="com.excellence.components.survey.domain.SurveyItem"
not-null="true"
lazy="false"
/>
<many-to-one
name="participant"
column="PARTICIPANT_ID"
class="com.excellence.components.common.User"
not-null="true"
lazy="false"
/>
<many-to-one
name="answer"
column="ANSWER_ID"
class="com.excellence.components.survey.domain.Answer"
lazy="false"
/>
<property
name="otherAnswer"
type="java.lang.String"
column="OTHER_ANSWER"
length="1024"
/>

</class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="com.excellence.components.survey.domain.SurveyItem" table="T_SURVEY_ITEM" lazy="false">

<id name="id" type="java.lang.Integer" column="ID" length="10">
<generator class="native"/>
</id>

<property
name="uuid"
type="java.lang.String"
column="UUID"
length="32"
/>
<many-to-one
name="questionnaire"
column="QUESTIONNAIRE_ID"
class="com.excellence.components.survey.domain.Questionnaire"
not-null="true"
lazy="false"
/>
<many-to-one
name="question"
column="QUESTION_ID"
class="com.excellence.components.survey.domain.Question"
not-null="true"
lazy="false"
/>
<property
name="required"
type="boolean"
column="REQUIRED"
/>
<property
name="sequenceNumber"
type="int"
column="SEQUENCE_NUMBER"
length="10"
/>
</class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="com.excellence.components.survey.domain.Questionnaire" table="T_SURVEY_QUESTIONNAIRE" lazy="false">

<id name="id" type="java.lang.Integer" column="ID" length="10">
<generator class="native"/>
</id>

<property
name="uuid"
type="java.lang.String"
column="UUID"
length="32"
/>
<property
name="title"
type="java.lang.String"
column="TITLE"
length="512"
/>
<many-to-one
name="department"
column="DEPARTMENT_ID"
class="com.excellence.components.common.Department"
not-null="false"
lazy="false"
/>
<many-to-one
name="creator"
column="CREATOR_ID"
class="com.excellence.components.common.User"
not-null="false"
lazy="false"
/>
<property
name="createDate"
type="java.util.Date"
column="CREATE_DATE"
length="23"
/>
<property
name="issueDate"
type="java.util.Date"
column="ISSUE_DATE"
length="23"
/>
<property
name="expiringDate"
type="java.util.Date"
column="EXPIRING_DATE"
length="23"
/>
<property
name="status"
type="byte"
column="STATUS"
length="1"
/>
<property
name="ballot"
type="int"
column="BALLOT"
length="10"
/>
<property
name="description"
type="java.lang.String"
column="DESCRIPTION"
length="1024"
/>
<!--
<set
name="surveyItems"
cascade="all"
order-by="SEQUENCE_NUMBER"
inverse="true"
lazy="false"
>
<key column="QUESTIONNAIRE_ID" not-null="true"/>
<one-to-many class="com.excellence.components.survey.domain.SurveyItem"/>
</set>
<set
name="surveyScope"
cascade="all"
order-by="SEQUENCE_NUMBER"
inverse="true"
lazy="false"
>
<key column="QUESTIONNAIRE_ID" not-null="true"/>
<one-to-many class="com.excellence.components.survey.domain.SurveyTarget"/>
</set>
-->
</class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="com.excellence.components.survey.domain.Question" table="T_SURVEY_QUESTION" lazy="false">

<id name="id" type="java.lang.Integer" column="ID" length="10">
<generator class="native"/>
</id>

<property
name="uuid"
type="java.lang.String"
column="UUID"
length="32"
/>
<property
name="description"
type="java.lang.String"
column="DESCRIPTION"
length="512"
/>
<property
name="type"
type="int"
column="TYPE"
length="10"
/>
<property
name="status"
type="byte"
column="STATUS"
length="1"
/>
<property
name="answerRows"
type="int"
column="ANSWER_ROWS"
/>
<property
name="layoutMode"
type="int"
column="LAYOUT_MODE"
/>
<many-to-one
name="department"
column="DEPARTMENT_ID"
class="com.excellence.components.common.Department"
not-null="false"
lazy="false"
/>
<!--
<set
name="answers"
cascade="all"
order-by="SEQUENCE_NUMBER"
inverse="true"
lazy="false"
>
<key column="QUESTION_ID" not-null="true"/>
<one-to-many class="com.excellence.components.survey.domain.Answer"/>
</set>
-->
</class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

<class name="com.excellence.components.survey.domain.Answer" table="T_SURVEY_ANSWER" lazy="false">

<id name="id" type="java.lang.Integer" column="ID" length = "10">
<generator class="native"/>
</id>

<property
name="uuid"
type="java.lang.String"
column="UUID"
length="32"
/>
<many-to-one
name="question"
column="QUESTION_ID"
class="com.excellence.components.survey.domain.Question"
not-null="true"
lazy="false"
/>
<property
name="value"
type="java.lang.String"
column="VALUE"
length="512"
/>
<property
name="answerable"
type="boolean"
column="ANSWERABLE"
/>
<property
name="answerRows"
type="int"
column="ANSWER_ROWS"
/>
<property
name="sequenceNumber"
type="int"
column="SEQUENCE_NUMBER"
length="10"
/>

</class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

<class name="com.excellence.components.survey.domain.SurveyTarget" table="T_SURVEY_TARGET" lazy="false">

<id name="id" type="java.lang.Integer" column="ID" length = "10">
<generator class="native"/>
</id>

<many-to-one
name="questionnaire"
column="QUESTIONNAIRE_ID"
class="com.excellence.components.survey.domain.Questionnaire"
not-null="true"
lazy="false"
/>
<many-to-one
name="surveyTarget"
column="SURVEY_TARGET_ID"
class="com.excellence.components.common.Role"
not-null="true"
lazy="false"
/>
<property
name="sequenceNumber"
type="int"
column="SEQUENCE_NUMBER"
length="10"
/>
<property
name="status"
type="int"
column="STATUS"
length="10"
/>

</class>
</hibernate-mapping>

xuexijava 2009-08-05
  • 打赏
  • 举报
回复
关注学习加顶
ABAP1314 2009-08-05
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 robyjeffding 的回复:]
引用 3 楼 sun914 的回复:
引用 1 楼 javagxc 的回复:
调查问卷,这很多的复选框,最好是每个复选框在数据库中都对应一个字段,这个字段的默认值是1,然后有人 参加的时候,就可以改变各个字段的状态,没选择的就不改变,这样的调查问卷,统计方便,可以随意进行统计都没问题,到时候扩展也不用改很多代码.


我是在 数据库中 建立 两个表 一个用户表  一个事所需要的复选框内容表    再建立一个 两个表的中间表 保存各表的ID      一个用户可以对应 多个复选框    一个复选框又对应多个用户

这样思路是这样的,主要看你映射文件的映射写好没有,你是单向多对多,还是双向多对多的啊?
[/Quote]

单向的
meander 2009-08-04
  • 打赏
  • 举报
回复
多对多利用中间表拆分为两个多对一。
jackingod 2009-08-04
  • 打赏
  • 举报
回复
一般多对多是要有中间表的啦。
robyjeffding 2009-08-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sun914 的回复:]
引用 1 楼 javagxc 的回复:
调查问卷,这很多的复选框,最好是每个复选框在数据库中都对应一个字段,这个字段的默认值是1,然后有人 参加的时候,就可以改变各个字段的状态,没选择的就不改变,这样的调查问卷,统计方便,可以随意进行统计都没问题,到时候扩展也不用改很多代码.


我是在 数据库中 建立 两个表 一个用户表  一个事所需要的复选框内容表    再建立一个 两个表的中间表 保存各表的ID      一个用户可以对应 多个复选框    一个复选框又对应多个用户
[/Quote]
这样思路是这样的,主要看你映射文件的映射写好没有,你是单向多对多,还是双向多对多的啊?
meadking 2009-08-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xblue3 的回复:]
问卷一个表
问题一个表(问题类型,复选框,选择题,多选,单选)
每个题目的条目(选择题的item选项)一个表
[/Quote]
你要做一些页面用于
问卷的设计,

另外一些用于问卷展示!让用户提交结果!
ABAP1314 2009-08-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 javagxc 的回复:]
调查问卷,这很多的复选框,最好是每个复选框在数据库中都对应一个字段,这个字段的默认值是1,然后有人 参加的时候,就可以改变各个字段的状态,没选择的就不改变,这样的调查问卷,统计方便,可以随意进行统计都没问题,到时候扩展也不用改很多代码.
[/Quote]

我是在 数据库中 建立 两个表 一个用户表 一个事所需要的复选框内容表 再建立一个 两个表的中间表 保存各表的ID 一个用户可以对应 多个复选框 一个复选框又对应多个用户
meadking 2009-08-04
  • 打赏
  • 举报
回复
问卷一个表
问题一个表(问题类型,复选框,选择题,多选,单选)
每个题目的条目(选择题的item选项)一个表

结果另外处理...放到另外的表中或者放在item条目表中的字段也可.

以前做过一个!代码扔掉了
javagxc 2009-08-04
  • 打赏
  • 举报
回复
调查问卷,这很多的复选框,最好是每个复选框在数据库中都对应一个字段,这个字段的默认值是1,然后有人 参加的时候,就可以改变各个字段的状态,没选择的就不改变,这样的调查问卷,统计方便,可以随意进行统计都没问题,到时候扩展也不用改很多代码.

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧