日期:2014-05-20  浏览次数:20907 次

联合组建,做外键问题........
我先说结果吧:表能创建,查询的时候不能转换!
关系:TCharge_Rule里面的 func_id 与charge_codes 是联合组建,但是charge_codes与TCharge 又是ManyToOne的关系
 以下是几个表的信息:
table 1: get set 省略
@Entity
public class TCharge implements Serializable {
private String charge_code; //PK
private String charge_name; //费用名称
private double charge; //业务费用
  @Id
  @GeneratedValue
  public String getCharge_code()
  {
  return charge_code;
  }
}

table 2: get set 省略  
@Entity  
@IdClass(TCharge_Rule_PK.class)
public class TCharge_Rule implements Serializable {
private String func_id;
private String func_name; //功能名称
private TCharge charge_codes;

  @Id
  public String getFunc_id()
  {
return func_id;
  }

  @Id
  @ManyToOne
  @JoinColumns
  (
{
@JoinColumn(name="charge_codes" , referencedColumnName="charge_codes")
}
  )
  public TCharge getCharge_codes() {
return charge_codes;
}
}

table 3 : 外键表 get set 省略
public class TCharge_Rule_PK implements Serializable{

private String func_id;
private TCharge charge_codes;
}

public void test(){
  List<TCharge_Rule> trList = Get("select * from TCharge_Rule");//查出了所有的数据
  TCharge_Rule tcr = null;
  for(int i = 0; i < trList.size(); i++)
{
tcr = trList.get(i); //此处报错:java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.model.TCharge_Rule
  }
}

求教高手!!谢谢~~~

------解决方案--------------------
Get("select * from TCharge_Rule");<-----Get是一个方法吗?
如果是的话尽量不要只用Get,另外你这里查出所有数据条目都要先转型为TCharge_Rule再放到trList里
tcr = (TCharge_Rule)trList.get(i); 
要放入tcr之前可以再转型试试
------解决方案--------------------
不知道LZ的Get方法怎么实现的,可以自己测试一下,是否trList里保存的对象都是TCharge_Rule
Java code
for(int i = 0; i < trList.size(); i++) {
    if (trList.get(i) != null) {
        System.out.println(trList.get(i).getClass());
    } else {
        System.out.println(trList.get(i));
    }
    //tcr = trList.get(i); 
}