hibernate动态建表实例解决

来源:互联网 发布:征途人物数据修改器 编辑:程序博客网 时间:2024/06/13 08:56

我这个程序的大体初衷就是在tomcat启动当中,在不重启tomcat的情况下,让hibernate帮我们动态建表,具体的实例就是:

注意下面的@Resource,这个非常重要,必须是这样,才能够获得正确的AnnotationSessionFactoryBean

 

 

@Controller
public class CustomAction extends BaseController {

 @Resource
 private DataSource dataSource;
 @Resource
 private AnnotationSessionFactoryBean annotationSessionFactoryBean;

 

 

下面就是Controller 的方法体了,


 @RequestMapping("test111")
 public String execute() throws MappingException, IOException,
   ClassNotFoundException {
  SessionFactoryImpl sessionFactory = (SessionFactoryImpl) this.baseDao
    .getSessionFactory();
  AnnotationConfiguration config = (AnnotationConfiguration) annotationSessionFactoryBean
    .getConfiguration();

  config.addAnnotatedClass(User9999.class);
  Settings settings = sessionFactory.getSettings();
  if (settings.isAutoUpdateSchema()) {
   new SchemaUpdate(config,settings).execute(false, true);
  }
  return "test111";
 }

 

 

 

原创粉丝点击