我实现了以下休眠对象:
@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 对象实现中有什么问题的任何想法???