0

我实现了以下休眠对象:

@Entity
@Table(name = "imageviewer_crvariable")
public class CRVariable implements Serializable   {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
@Column(name = "VarId")
private Long varId;

@Column(name = "VarName")
private String varName;

@Column(name = "VarDescription")
private String varDescription;

@ManyToMany(mappedBy="crvariables")
private Set<CRImageType> crimagetypes = new HashSet<CRImageType>();

public CRVariable(){
}

public CRVariable(Long varId,String varName, String varDescription){
    this.varId=varId;
    this.varName=varName;
    this.varDescription=varDescription;
}

//getters and setters 

我还有一个由 back bean 管理的 primefaces 选项列表组件。我已经实现了一个方法“setAssocImTypeVariables”,它使用新的选项列表值(在列表项从源传输到目标之后)更新我的休眠对象(CRVariable),如下所示:

public void setAssocImTypVariables() {      
    dualListVars = getDualListVars();       
    List<CRVariable> sourceVariables = dualListVars.getSource();
    List<CRVariable> targetVariables = dualListVars.getTarget();

    for (int i=0; i<sourceVariables.size(); i++) {
        System.out.println(">>>>>>>>>>>>> I am a source variable: " + sourceVariables.get(i));
    }

    for (int i=0; i<targetVariables.size(); i++) {
        System.out.println(">>>>>>>>>>>>> I am a target variable: " + targetVariables.get(i));
    }

    CRImageType selectedImageType = getSelectedCRImageType();
    Long selImTypeId = selectedImageType.getImTypeId();

这段代码运行良好,这是我在控制台中得到的结果:

>>>>>>>>>>>>> I am a source variable: varId : 75, varName : Ki67(X), varDescription : Ki67(X)
>>>>>>>>>>>>> I am a source variable: varId : 76, varName : PTEN(X), varDescription : PTEN(X)
>>>>>>>>>>>>> I am a source variable: varId : 77, varName : Histology (X), varDescription : Histology (X)
>>>>>>>>>>>>> I am a target variable: varId : 72, varName : HER2(X), varDescription : HER2(X)
>>>>>>>>>>>>> I am a target variable: varId : 71, varName : ER (X), varDescription : ER (X)
>>>>>>>>>>>>> I am a target variable: varId : 74, varName : FISH, varDescription : FISH description

但是,当我尝试为 targetVariables 列表的每个对象获取 VarId 时,如下所示:

System.out.println(">>>>>>>>>>>>> I am a target variable: " + targetVariables.get(i).getVarId());

我收到以下错误:

>>>>>>>>>>>>> I am a target variable: varId : 72, varName : HER2(X), varDescription : HER2(X)
java.lang.ClassCastException: java.lang.String cannot be cast to com.forth.dbutils.CRVariable
    at com.forth.actions.ReportConfiguratorBean.setAssocImTypVariables(ReportConfiguratorBean.java:414)

第 414 行是我试图获取当前对象列表的 VarId 属性的行:targetVariables.get(i).getVarId()

 System.out.println(">>>>>>>>>>>>> I am a target variable: " + targetVariables.get(i).getVarId());

关于我的 Hibernate 对象实现中有什么问题的任何想法???

4

1 回答 1

0

我的选择列表组件:

<p:pickList id="pickList" 
  value="#   {reportConfiguratorBean.dualListVars}"
  var="cRVariable" 
  itemValue="#{cRVariable}" 
  itemLabel="#{cRVariable.varName}"
  converter="cRVariableConverter"
  Ajax="false" >
  <f:facet name="sourceCaption">Available Variables</f:facet>
  <f:facet name="targetCaption">Associated Variables</f:facet>              
</p:pickList>

和 cRVariableConverter 以获取 CRVariable 目标列表项作为对象:

public Object getAsObject(FacesContext context, UIComponent component, String value) {
    Long varId = Long.parseLong(value.trim());  
    List<CRVariable> cRVariables = mybean.getCRVariables();

    for (CRVariable cRVariable:cRVariables) {
        if(cRVariable.getVarId().compareTo(varId)==0){
            return cRVariable;
        }
    }

    return null;    
}
于 2012-11-29T12:50:04.477 回答