Question

I am trying to create a SessionFactory bean. My code is as follows:

@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;
}

The exception that is throws is as follows:

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

Solution

When you are using inheritance in POJO super class should have the uniqueId as the primary key and the sub classess can only use it as foreign key if you do not do it that way i feel u land in this exception. Try making the subclass primary key different and also provide a foreign key connection between the super class and the subclass. Check this is a good example that is solved: Spring 3.1 Hibernate 4 exception for Inheritance [cannot be cast to org.hibernate.mapping.RootClass]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top