Hibernate如何在启动时创建表? [英] How to have Hibernate create tables on startup?

查看:62
本文介绍了Hibernate如何在启动时创建表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Hibernate与HSQLDB一起使用,并且我已经注意到,当应用程序启动时,在HSQLDB中不会创建任何表.直到第一次选择或第一次插入时,所有表才在HSQLDB中创建.在应用程序启动时,有什么方法可以创建所有表吗?

I am using Hibernate with HSQLDB and I have noticed that when the application starts up, no tables get created in HSQLDB. It's not until the first select or the first insert that all the tables get created in HSQLDB. Is there any way to have all the tables get created when the application starts up?

编辑以回复Pangea:

EDIT in reply to Pangea:

我修改了我们的休眠cfg xml文件,使其包含以下内容:

I modified our hibernate cfg xml file so that it includes the following:

<property name="hbm2ddl.auto">create</property>
<prop key="hibernate.hbm2ddl.auto">create</property>

但是,当服务器启动时,仍然无法创建表.它们仅在第一次插入或选择后创建.您提到了将在创建Hibernate会话工厂期间创建表.在执行插入或选择之前,我们的代码不会调用Configuration.buildSessionFactory().有什么方法可以在此之前创建表?

But the tables still do not get created when the server starts up. They only get created after the first insert or select. You mentioned that the tables will be created during the creation of the Hibernate session factory. Our code does not call the Configuration.buildSessionFactory() until an insert or a select is performed. Is there any way to create the tables prior to that?

推荐答案

一般来说,休眠是这样工作的.当它被调用时启动.您可以创建一个空的构造函数,并在应用程序的开头对其进行调用,或者在您通过Servlet调用使用Web应用程序时在web.xml中启动它.

Well that is how hibernate generally works. It is started when it is called. You could create an empty constructor and have it called at the beginning of your application or have it started up within your web.xml is you are using a web application with a servlet call.

如果您不让程序知道要在程序启动时启动休眠模式,则直到被告知SessionFactory或会话工厂需要它时,它才会创建它.

If you don't let your program know that you want hibernate to start at the beginning of you start of the program it will not create it until it has been told to or is needed for the SessionFactory.

据我所知,您拥有所有正确的语法.只需确保在程序开始时的其他位置,希望您可以更早调用它即可.

You have all the correct syntax from what I can tell. Just ensure else in the beginning of the program that you want it to be called earlier.

Hibernate.org有一个HibernateUtil.class的创建示例,您可以对其进行修改以添加一个空的构造函数以供调用.

Hibernate.org has a create example of HibernateUtil.class which you can modify to add an empty constructor to call upon.

希望这有助于在应用程序开始时启动Hibernate.

Hope this helps with starting up Hibernate at the beginning of your application.

这篇关于Hibernate如何在启动时创建表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆