SQL中的CheckBox Multiple Choice商店

时间:2011-04-21 23:40:32

标签: java sql hibernate checkbox wicket

我使用wicket 1.3 CheckBoxMultipleChoice()方法让用户为项目选择相关团队。当我将团队列表存储到数据库时,它将使用此会话特有的标识符进行存储,例如:[info.tpath.domain.Team@1c3d514,info.tpath.domain.Team @ 1510241,info.tpath.domain.Team @ 1d26ddd,info.tpath.domain.Team @ ea423e]。有没有办法劫持所选项目列表,以便将对象ID存储为:Team.getId();?非常感谢任何帮助...

我想使用hibernate将团队列表存储为MS SQL08数据库中的字符串。

    List<Team> choices = new ArrayList<Team>();
    for(int i=1;i<5;i++){
        for(Team team:getJtrac().findTeamGroup(i)){
            choices.add(team);
        }
    }
    CheckBoxMultipleChoice pcrTeamz = new CheckBoxMultipleChoice("pcrTeams", choices, new IChoiceRenderer() {
        public Object getDisplayValue(Object o) {
            return ((Team) o).getName();
        }
        public String getIdValue(Object o, int i) {
            long lTeam = ((Team) o).getId();
            return Long.toString(lTeam);
        }
    });
    add(pcrTeamz);

表单的onSubmit()如下:

 @Override
    protected void onSubmit() {
        ManagementOfChange managementOfChange = (ManagementOfChange) getModelObject();
        managementOfChange.setStatus(status);
        managementOfChange.setProject(project);
        managementOfChange.setLoggedBy(getPrincipal());
        getJtrac().storeManagementOfChange(managementOfChange);
        setResponsePage( new ProjectPage(project, new ManagementOfChangeSubSectionPanel("projectSubSectionPanel",project)));
    }

在下面的storeManagementOfChange()方法中,dao.storeManagementOfChange(moc)只调用getHibernateTemplate()。merge(moc);

public void storeManagementOfChange(ManagementOfChange moc) {
        History history = new History(moc);
        Date now = new Date();
        moc.setTimeStamp(now);
        history.setTimeStamp(now);
        history.setLoggedBy(moc.getEnteredBy());
        if(history.getComment()==null){
            history.setComment("Creation of New PCR");
        }
        moc.add(history);
        SpaceSequence spaceSequence = dao.loadSpaceSequence(moc.project.getProjectId());
        moc.setPcrNum(spaceSequence.nextPcr());
        // the synchronize for this storeItem method and the hibernate flush() call in the dao implementation
        // are important to prevent duplicate sequence numbers
        dao.storeSpaceSequence(spaceSequence);
        //this call should not be required actually, but the cascase setting has some problem probably
        //because we are using a polymorphic association between a moc and history. that is why we
        //are explicitly saving history before actually saving the moc itself.
        dao.storeHistory(history);
        // this will at the moment execute unnecessary updates (bug in Hibernate handling of "version" property)
        // see http://opensource.atlassian.com/projects/hibernate/browse/HHH-1401
        // TODO confirm if above does not happen anymore
        dao.storeManagementOfChange(moc);
        indexer.index(moc);
        indexer.index(history);
        mailSender.send(moc, moc.isSendNotifications());
    }

最后,hibernate映射如下:

 <class name="ManagementOfChange" table="management_of_change">
    <id column="id" name="id">
      <generator class="native"/>
    </id>
    <many-to-one column="project_id" index="idx_project_id" name="project" not-null="true"/>
    <property column="requester" name="requester"/>
    <property column="phase" name="phase"/>
    <property column="description" name="description"/>        
    <property column="third_party" name="thirdParty"/>
    <many-to-one column="entered_by" index="idx_user_id" name="enteredBy" not-null="true"/>
    <property column="internal_or_external" name="source"/>
    <property column="change_number" name="changeNum"/>
    <property column="pcr_number" name="pcrNum"/>
    <property column="milestone_affected" name="milestoneAffected"/>
    <property column="new_due_date" name="newDueDate"/>
    <property column="pcr_group_num" name="pcrGroupingNumber"/>
    <property column="pcr_title" name="pcrTitle"/>
    <property column="status" name="status"/>
    <property column="time_estimate" name="timeEstimate"/>
    <property column="teams" name="pcrTeams"/>
    <property column="timestamp" name="timestamp"/>
    <property column="sow" name="sow"/>
    <property column="req_date" name="reqDate"/>
  </class>

2 个答案:

答案 0 :(得分:1)

CheckBoxMultipleChoice的模型对象是List<Team>,而不是List<Long>,因此您将持久化整个Team对象。如果有的话,您的实体似乎没有正确映射到您的数据库。

我猜你有几个选择:

  1. 修复您的JPA / JDO映射(首选)
  2. 在您的表单提交中,请勿坚持pcrTeamz.getModelObject()。而是从每个Team对象中提取id并保留该列表。
  3. 例如:

    List<Long> teamIds = new ArrayList<Long>();
    for(Team team : pcrTeamz.getModelObject()) {
       teamIds.add(team.getId());
    }
    myBO.save(teamIds);
    

答案 1 :(得分:0)

解决方案最终是将CheckBoxMultipleChoice传递给List而不是List。然后,所选字符串列表将合并到数据库,而不是会话对象标识符列表。我从来没有成功地从CheckBoxMultipleChoice中提取团队对象。如果有人知道如何做到这一点我会感兴趣。谢谢!

    // associated team list =================================================
    List<String> choices = new ArrayList<String>(); //init as List<String>
    for(int i=1;i<5;i++){
        for(Team team:getJtrac().findTeamGroup(i)){
            choices.add(team.getName()); //extract team names to List<String>
        }
    }
    pcrTeamz = new JtracCheckBoxMultipleChoice("pcrTeams", choices, new IChoiceRenderer() {
        public Object getDisplayValue(Object o) {
            return o;
        }
        public String getIdValue(Object o, int i) {
            return o.toString();
        }
    });
    add(pcrTeamz);