开发者

CheckBoxMultipleChoice store in SQL

开发者 https://www.devze.com 2023-02-27 15:32 出处:网络
I am using the wicket 1.3 CheckBoxMultipleChoice() method to have the user select relevant teams for a project.When I store the team list to the database it gets stored using identifiers unique to thi

I am using the wicket 1.3 CheckBoxMultipleChoice() method to have the user select relevant teams for a project. When I store the team list to the database it gets stored using identifiers unique to this session such as: [info.tpath.domain.Team@1c3d514, info.tpath.domain.Team@1510241, info.tpath.domain.Team@1d26ddd, info.tpath.domain.Team@ea423e]. Is there a way to hijack the list of selected items so as to have it store the object id as in: Team.getId();? Any assistance is greatly appreciated...

I want to store the team list as a string in the MS SQL08 DB using hibernate.

    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);

The onSubmit() for the form is below:

 @Override
    p开发者_开发知识库rotected 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)));
    }

In the storeManagementOfChange() method below, the dao.storeManagementOfChange(moc) just calls 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());
    }

And finally, the hibernate mappings are below:

 <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>


The model object of your CheckBoxMultipleChoice is a List<Team>, not a List<Long>, so you are persisting the entire Team objects. It appears that your entities are not properly mapped to your database, if at all.

I guess you have a couple of options:

  1. fix your JPA/JDO mappings (preferred)
  2. in your form submit, do not persist pcrTeamz.getModelObject(). Instead extract the id out of each Team object and persist that list.

For example:

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


The solution ended up being to pass the CheckBoxMultipleChoice a List as opposed to a List. Then the list of selected strings gets merged to the database instead a list of session object identifiers. I was never successful in extracting the team objects out of the CheckBoxMultipleChoice. If anyone knows how to do this I would be interested. Thanks!

    // 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);
0

精彩评论

暂无评论...
验证码 换一张
取 消