Hibernate:从 Java 代码开始数据库布局
假设有以下简单类:
package Sample;
public class Status {
private Long id;
private String status;
public Status() {}
public Status(String status) {
this.status = status;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
对于该类,我已在 Status.hbm.xml
中定义了 hibernate 映射。
该文件向 Hibernate 描述了如何将类映射到表。
在 hibernate.cfg 中,我定义了如何连接到我的数据库 (MySQL)。
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.username">root</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="sample/Status.hbm.xml"/>
</session-factory>
</hibernate-configuration>
我不明白的是以下内容:
使用此配置,当我尝试使用 org.hibernate.Session 将 Status 对象保存到数据库时,它抱怨没有可连接的数据库。
842 [main] 错误 org.hibernate.util.JDBCExceptionReporter - 否 已选择数据库
添加为 hibernate.cfg.xml
的 URL 连接字符串的一部分,即
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
我收到一个不同的错误,抱怨表 test.status
没有存在。
我期待 Hibernate 会创建它。
只有当我这样做时,代码才是成功的:
SchemaExport schemaExport = new SchemaExport(cfg);
schemaExport.create(false, true);
所以我的问题是,使用 Hibernate 并从 Java 开始创建模式、数据库及其表的正确过程是什么。
我不想在 MySQL 中手动创建数据库,例如从 MySQL 客户端。
上述使用 SchemaExport
的方法是否正确?
从 Java DB 设计开始的最佳选择是什么?
Assume the following trivial class:
package Sample;
public class Status {
private Long id;
private String status;
public Status() {}
public Status(String status) {
this.status = status;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
For this class I have defined the hibernate-mapping in the Status.hbm.xml
.
This file, describes to Hibernate how to map the class to a table.
In the hibernate.cfg
I have defined how to connect to my Database (MySQL).
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.username">root</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="sample/Status.hbm.xml"/>
</session-factory>
</hibernate-configuration>
What I do not undertand is the following:
With this configuration when I try to use the org.hibernate.Session
to save an object of Status
to the database, it complaints that there is no database to connect.
842 [main] ERROR org.hibernate.util.JDBCExceptionReporter - No
database selected
If I add an existing database as part of the URL connection string of hibernate.cfg.xml
i.e.
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
I get a different error complaining that the table test.status
does not exist.
I was expecting that Hibernate would create it.
The code is succesfull only if I do:
SchemaExport schemaExport = new SchemaExport(cfg);
schemaExport.create(false, true);
So my question is, what is the proper process to use Hibernate and create a schema and a database and its tables starting from Java.
I do not want to manually create a database in MySQL for example from an MySQL client.
Is the above approach with the SchemaExport
the correct way?
What is the best option for the start from Java DB design?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让 Hibernate 创建表结构对于测试来说很好,但是一旦应用程序部署到生产环境并实际运行,您将不希望 Hibernate 每次都创建数据库您重新部署,因为它会清除您的数据。习惯创建
ALTER
脚本来管理数据库更改是一个好习惯,因为大多数开发都不是新的,而是更新现有系统。话虽这么说,是的,您所指出的方式是让 Hibernate 创建数据库模式的正确方式。您还可以在 Hibernate 配置文件中设置这种情况,但我不记得这个属性了。
Having Hibernate create the table structure is fine for testing, but once the application has been deployed to production and is actually running, you will NOT want hibernate to create the database every time you re-deploy because it will wipe out your data. Getting used to creating
ALTER
scripts for managing database changes is a good habit to get into, since most development is not new but is updating an existing system.That being said, yes, the way you have indicated is the correct way to have Hibernate create the database schema. You can also set that to happen in the Hibernate config file, but I don't remember the property off the top of my head.