SQL 中的 CheckBoxMultipleChoice 存储
我正在使用 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 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);
表单的 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());
}
最后,休眠映射如下:
<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>
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
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)));
}
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CheckBoxMultipleChoice 的模型对象是
List
,而不是List
,因此您要保留整个 Team 对象。看来您的实体没有正确映射到数据库(如果有的话)。我想您有几个选择:
pcrTeamz.getModelObject()
。相反,从每个团队对象中提取 ID 并保留该列表。例如:
The model object of your CheckBoxMultipleChoice is a
List<Team>
, not aList<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:
pcrTeamz.getModelObject()
. Instead extract the id out of each Team object and persist that list.For example:
解决方案最终是向 CheckBoxMultipleChoice 传递一个列表而不是列表。然后,所选字符串的列表将合并到数据库中,而不是会话对象标识符的列表。我从未成功地从 CheckBoxMultipleChoice 中提取团队对象。如果有人知道如何做到这一点,我会很感兴趣。谢谢!
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!