2013-04-01 157 views
-1

我想在应用程序服务器启动时创建数据库模式。
我使用带注释和hibernate.cfg.xml的hebernate 4.1.9进行配置。休眠数据库创建

所以问题是我不能完全理解我应该如何创建模式,然后在应用程序中使用它。当然,我只想在第一次启动和下一次启动时执行模式创建,我想更新它。

我想在更新状态使用hbn2ddl.auto,但数据库不会创建。可能是我应该在hibernate.connection.url的末尾使用诸如INIT=create schema IF NOT EXISTS myschema之类的东西。

我也有一个例外

org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set 

但在hibernate.cfg.xml有这样的字符串:

<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> 

可有人形容它给我吗?

+0

试试看[这个问题](http://stackoverflow.com/questions/12373944/how-to-create-database-schema-with-hibernate-cfg -xml)以及其答案中的链接。 –

+0

我之前看到了答案中的链接,但它不起作用,因为我不想正确添加其他创建脚本,并且将INIT块添加到URL中不会创建该模式。其实我试图理解为什么? – Anatoly

+0

Hibernate文档仅将** hibernate.connection.url **定义为JDCB URL。 [MySQL连接器/ J参考](http://dev.mysql.com/doc/refman/5.5/en/connector-j-reference-configuration-properties.html)没有定义** INIT **属性。 我相信链接文章的重点是你必须咬紧牙关,至少自己创建一个空的模式。 –

回答

2

我自己解决了这个问题。我应该添加createDatabase = true hibernate.connection.url

+0

Spiffy。 'createDatabaseIfNotExist = true'是另一种选择。 –

+0

当我谈到INIT财产时,我的意思是我试图找到类似的东西。所以我在你的链接中找到了它。谢谢。 – Anatoly