1

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
h-rai
  • 3,636
  • 6
  • 52
  • 76
  • Do u create bean for anyother pojo – muthu Mar 20 '13 at 06:19
  • I dont think the problem is with session factory check your bean files.. as i found this http://stackoverflow.com/q/3615778/2006839 and http://forum.springsource.org/showthread.php?123978-Inheritence-polymorphism-and-Id-problem post your bean class.. – Lakshmi Mar 20 '13 at 06:25
  • @Lakshmi I have tried looking at the answer earlier but I don't quite get it. – h-rai Mar 20 '13 at 06:30
  • @nick-s check if you are using inheritance in that case your 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. you can check this also http://stackoverflow.com/q/14806400/2006839 and http://stackoverflow.com/q/10948539/2006839. Why not autowire sessionFactory in your class rather than manually configuring it? – Lakshmi Mar 20 '13 at 06:40
  • @Lakshmi you are right. I have a super class which has a primary key and also it's subclasses which use primary key. I will see what I can do in that case. – h-rai Mar 20 '13 at 06:57

1 Answers1

5

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]

Community
  • 1
  • 1
Lakshmi
  • 2,204
  • 3
  • 29
  • 49