在sessionFactory定义中,您可以定义模型位于哪些包中。将此属性添加到您的
<session-factory>:
<property name="packagesToScan" value="com.suvrat"/>
或者,您可以将此类显式添加到Java中的SessionFactory定义中:
SessionFactory factory = new Configuration().configure("/resources/hibernate.cfg.xml") .addAnnotatedClass(Tutor.class) .addAnnotatedClass(User.class) .buildSessionFactory();在大多数情况下,您将使用包裹扫描方式。
评论 :
因为您使用的是Spring,所以建议将SessionFactory提取到另一个@Configuration,然后仅将其注入Controller中。您的应用程序应该只有一个SessionFactory。其次,在没有任何数据库访问的情况下,测试控制器要容易得多。
因此,像这样创建一个HibernateConfig:
@Configurationpublic class HibernateConfig { @Bean public SessionFactory sessionFactory(){ return new Configuration().configure("/resources/hibernate.cfg.xml") .addAnnotatedClass(Tutor.class) .addAnnotatedClass(User.class) .buildSessionFactory(); }}然后像这样更改您的控制器:
@Controllerpublic class HandleTutor { @Autowired private SessionFactory sessionFactory;}


