当我想从表中获取数据时,我收到了这个错误。谁能帮我?

时间:2016-08-15 19:14:05

标签: hibernate

错误: Session类型中的get(Class,Serializable)方法不适用于参数(Class,int)?

public class Test {

    public static void main(String[] args) {

        Configuration config = new Configuration().configure();
        SessionFactory factory = config.buildSessionFactory();
        Session session = factory.openSession();
        Transaction trx = session.beginTransaction();
        Sample sample = new Sample();
        sample = (Sample)session.get(Sample.class, 1);
        trx.commit();
        System.out.println("success");
        session.close();
    }
}

public class Sample {
    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

1 个答案:

答案 0 :(得分:0)

你应该使用

...
sample = (Sample)session.get(Sample.class, new Integer(1));
...

而不是简单的int。