Java SQL中的CheckboxMultipleChice存储
我使用wicket 1.3 CheckboxMultipleChice()方法让用户为项目选择相关团队。当我将团队列表存储到数据库时,它将使用此会话特有的标识符进行存储,例如:[info.tpath.domain]。Team@1c3d514,info.tpath.domain。Team@1510241,info.tpath.domain。Team@1d26ddd,info.tpath.domain。Team@ea423e]. 是否有方法劫持所选项目的列表,以便将对象id存储为:Java SQL中的CheckboxMultipleChice存储,java,sql,hibernate,checkbox,wicket,Java,Sql,Hibernate,Checkbox,Wicket,我使用wicket 1.3 CheckboxMultipleChice()方法让用户为项目选择相关团队。当我将团队列表存储到数据库时,它将使用此会话特有的标识符进行存储,例如:[info.tpath.domain]。Team@1c3d514,info.tpath.domain。Team@1510241,info.tpath.domain。Team@1d26ddd,info.tpath.domain。Team@ea423e]. 是否有方法劫持所选项目的列表,以便将对象id存储为:Team.get
Team.getId()代码>?非常感谢您的帮助
我想使用hibernate将团队列表作为字符串存储在MS SQL08 DB中
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);
在下面的storeManagementOfChange()方法中,dao.storeManagementOfChange(moc)只调用getHibernateTemplate().merge(moc)
最后,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>
复选框MultipleChice的模型对象是一个列表,而不是列表,因此您要持久化整个团队对象。您的实体似乎没有正确映射到数据库(如果有的话)
我想你有几个选择:
修复JPA/JDO映射(首选)
在表单submit中,不要持久化pcrTeamz.getModelObject()
。相反,从每个团队对象中提取id并保存该列表
例如:
List<Long> teamIds = new ArrayList<Long>();
for(Team team : pcrTeamz.getModelObject()) {
teamIds.add(team.getId());
}
myBO.save(teamIds);
List teamIds=new ArrayList();
for(团队:pcrTeamz.getModelObject()){
添加(team.getId());
}
myBO.save(teamIds);
解决方案是通过复选框multipleechoice传递列表,而不是列表。然后,选定字符串的列表合并到数据库中,而不是会话对象标识符的列表。我从未成功地从CheckboxMultipleChice中提取团队对象。如果有人知道怎么做,我会感兴趣的。谢谢
// 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);
//关联的团队列表=================================================
列表选项=新建ArrayList()//初始化为列表
对于(int i=1;执行保存到DB的代码与您的问题更相关。如何防止保留原始pcrTeamz对象?pcrTeamz.getModelObject()返回如下字符串:“”[info.jtrac.domain]。Team@a1f4ae,info.jtrac.domain。Team@c6dc3e,info.jtrac.domain。Team@1a5a02f]"'如何将其返回到列表或团队对象?
List<Long> teamIds = new ArrayList<Long>();
for(Team team : pcrTeamz.getModelObject()) {
teamIds.add(team.getId());
}
myBO.save(teamIds);
// 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);