Hibernate Typecasting问题。

时间:2014-05-01 12:13:56

标签: java hibernate

在hibernate中将客户类强制转换为对象类时,我遇到了问题:

public class HibernateCustomerDAO implements CustomerDAO {
    @Override
    public int addCustomer(CustomerDTO cto) {
        Customer cust = new Customer(cto.getName(), cto.getEmail(), cto.getTelephone(), cto.getAge(), cto.getTime());
        Integer it = (Integer)HibernateTemplate.save(cust);
        return it.intValue();
    }
}

问题与下面给出的代码有关:

public static Object save(Object obj) {     
    Object o = null;
    try {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();
        o = session.save(obj);
        tx.commit();
        session.close();
    } catch(Exception e) {
        e.printStackTrace();
    }

    return o;
}

1 个答案:

答案 0 :(得分:1)

Session.save返回一个Serializable-Object。 (http://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/Session.html#save%28java.lang.Object%29

但是Object没有实现标记接口Serializable。 (http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html