SessionFactory bean创建失败(抛出ClassCastException)

时间:2013-03-20 06:06:44

标签: java hibernate spring-mvc sessionfactory

我正在尝试创建一个SessionFactory bean。我的代码如下:

@Bean
public SessionFactory sessionFactory() {
    return new LocalSessionFactoryBuilder(dataSource()).scanPackages("com.package").buildSessionFactory();
}

@Bean
public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/csu_library");
    dataSource.setUsername("csulibrary");
    dataSource.setPassword("csulibrary");
    dataSource.setMaxActive(10);
    dataSource.setMaxIdle(5);
    dataSource.setInitialSize(5);

    return dataSource;
}

抛出的异常如下:

Caused by: java.lang.ClassCastException: org.hibernate.mapping.SingleTableSubclass cannot be cast to org.hibernate.mapping.RootClass

1 个答案:

答案 0 :(得分:5)

当你在POJO中使用继承时,超类应该将uniqueId作为主键,而sub classess只能将它用作外键,如果你不这样做,我觉得你在这个异常中着陆。尝试使子类主键不同,并在超类和子类之间提供外键连接。检查这是一个很好的例子: Spring 3.1 Hibernate 4 exception for Inheritance [cannot be cast to org.hibernate.mapping.RootClass]