我正在尝试使用hibernate / GWT / Java以编程方式创建模式和表。我创建了包含所有映射资源和属性等的配置,
Configuration hibConfiguration = new Configuration().configure(configFileDoc);
但是当我说
时SchemaExport schemaExport = new SchemaExport(hibConfiguration);
schemaExport.create(true, true);
它抛出异常,
ERROR: HHH000231: Schema export unsuccessful
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'testschema'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:943)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4113)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1308)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2336)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.GeneratedConstructorAccessor36.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:193)
at org.hibernate.tool.hbm2ddl.ManagedProviderConnectionHelper.prepare(ManagedProviderConnectionHelper.java:55)
at org.hibernate.tool.hbm2ddl.DatabaseExporter.<init>(DatabaseExporter.java:52)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:367)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:304)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:293)
我的cfg.xml文件:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">passwd</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/testSchema</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="javax.persistence.validation.mode">none</property>
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
<property name="hibernate.default_entity_mode">dynamic-map</property>
<property name="hibernate.hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>
如果我手动创建了模式,那么它工作正常,即也创建表和列。但通常它不是创建模式,是不是可以自动创建模式?
答案 0 :(得分:1)
Hibernate不会动态创建架构......您必须手动创建它。我使用简单的JDBC连接和语句来创建模式,然后SchemaExport将完成生成表和字段的工作。所以没有办法使用hibernate创建实际的模式。你必须手动创建它。
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document configFileDoc = builder.parse(new ByteArrayInputStream(configFileStr.getBytes()));
Configuration hibConfiguration = new Configuration();
hibConfiguration.configure(configFileDoc);
ArrayList<Document> hbmFileDocs = getHBMFileDocList(); //created the documents of the file string
for(Document doc : hbmFileDocs)
hibConfiguration.addDocument(doc);
hibConfiguration.buildMappings();
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/", "username", "password");
stmt = conn.createStatement();
String sql = "CREATE DATABASE "+schemaName;
stmt.executeUpdate(sql);
SchemaExport schemaExport = new SchemaExport(hibConfiguration);
schemaExport.create(true, true);
这将生成完整的架构,它对我有用。
答案 1 :(得分:0)
您需要在hibernate配置文件中再添加一个属性,以自动创建数据库/模式:
<property name="hibernate.hbm2ddl.auto">create</property>
答案 2 :(得分:0)
我猜hibernate
仅在创建database
的情况下创建架构。您需要手动创建数据库testschema
。
手动创建数据库testschema
,然后启动hibernate应用程序。
答案 3 :(得分:-2)
可以自动创建模式。将以下属性添加到Hibernate config xml:
<property name="hibernate.hbm2ddl.auto">update</property>
或强>
<prop key="hibernate.hbm2ddl.auto">update</prop>
根据上述属性,Hibernate将仅创建数据库(如果尚不存在),并将更新任何现有表以匹配您当前的域模型。
另一个选择是:
<property name="hibernate.hbm2ddl.auto">create-drop</property>
上述属性意味着每次启动应用程序时Hibernate都会创建一个全新的数据库
更新:有用的网址
http://manikandanmv.wordpress.com/2011/04/13/hibernate-basics-simple-example/
http://blog.sencide.com/2011/03/hibernate-tutorial-for-beginners.html
http://www.jroller.com/eyallupu/entry/hibernate_s_hbm2ddl_tool
http://hannelita.wordpress.com/2012/03/21/hibernate-cfg-hibernate-4-0-0/