[英]Hibernate does not generate tables from models to database
我正在嘗試從models類中獲取包含表的數據庫,問題在於自動生成帶有Hibernate批注的表。
我有一個Maven模型Projet命名模型-測試 ,我有以下課程:
@Audited
@Entity
@Table(name = "test_EXTEND", schema = "COTest")
public class ComaTest implements Serializable {
private static final long serialVersionUID = 393765467600954104L;
@Id
@Column(name = "CL_o_CODE")
private Integer id;
@Column(name = "CL_o_NUM01")
private Long refCountryCode;
@Column(name = "CL_o_STR01")
private String city;
@Column(name = "CL_o_STR02")
private String zipCode;
public Integer getId() {
return this.id;
}
public void setId(final Integer idVal) {
this.id = idVal;
}
public Long getRefCountryCode() {
return this.refCountryCode;
}
public void setRefCountryCode(final Long refCountryCode) {
this.refCountryCode = refCountryCode;
}
public String getCity() {
return this.city;
}
public void setCity(final String cityVal) {
this.city = cityVal;
}
public String getZipCode() {
return this.zipCode;
}
public void setZipCode(final String zipCodeVal) {
this.zipCode = zipCodeVal;
}
}
我有另一個命名為interface_test的projets,其中有我所有的接口,然后還有另一個在persistence.xml fil中進行的maven projet命名服務-test:
<persistence-unit name="op_PU">
<jta-data-source>java:jboss/datasources/op_DS</jta-data-source>
<properties>
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="org.hibernate.envers.audit_table_suffix"
value="_aud" />
<property name="org.hibernate.envers.revision_field_name"
value="rev" />
<property name="org.hibernate.envers.revision_type_field_name"
value="rev_type" />
</properties>
</persistence-unit>
在jboss服務器的standalone.xml中,我具有以下連接字符串:
<datasource jndi-name="java:jboss/datasources/op_DS" pool-name="op_DS" enabled="true" use-java-context="true">
<connection-url>jdbc:sqlserver://localhost;databaseName=OP_DB;</connection-url>
<driver>sqlserver</driver>
<security>
<user-name>sa</user-name>
<password>testpassword</password>
</security>
</datasource>
在consol我有:
ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 94) The specified schema name "COTest" either does not exist or you do not have permission to use it.
ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 94) HHH000389: Unsuccessful: create table COTest.test_EXTEND (CL_o_CODE int not null, CL_o_STR01 varchar(255), CL_o_STR02 varchar(255), CL_o_STR03 varchar(255), primary key (CL_o_CODE, rev)))
嗨,我認為您的persistence.xml屬性名為hibernate.hbm2ddl.auto存在問題,您已為其提供了作為更新的值,而您需要的是創建或創建放置(如果是開發環境)以從模型到數據庫生成表。 hibernate.hbm2ddl.auto的可能選項的列表是,
validate: validate the schema, makes no changes to the database.
update: update the schema.
create: creates the schema, destroying previous data.
create-drop: drop the schema at the end of the session.
當我在SQL Server 2008 R2上手動創建架構時,hibernate可以從現有實體中完美地生成表。 這是關於持久性單元的代碼塊:
<persistence-unit name="OP_PU">
<jta-data-source>java:jboss/datasources/OP_DS</jta-data-source>
<properties>
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="org.hibernate.envers.audit_table_suffix"
value="_aud" />
<property name="org.hibernate.envers.revision_field_name"
value="rev" />
<property name="org.hibernate.envers.revision_type_field_name"
value="rev_type" />
</properties>
</persistence-unit>
但是我正在尋找一種解決方案,它可以幫助我自動創建帶有模式名稱的表,而無需手動創建模式(在生成表之前,必須先創建模式)是否有解決方案?
好的,在看完您的最新文章之后,我認為您必須做一些事情,我們才能在創建表之前創建模式:1.像在數據庫URL中添加“; INIT = create schema IF NOT EXISTS MYDBNAME”一樣,例如
database.url=jdbc:DB:USER:;INIT=create schema IF NOT EXISTS MYDBNAME.
您可以選擇使用文件路徑將RUNSCRIPT添加到INIT值
database.url=jdbc:DB:USER:;INIT=RUNSCRIPT FROM 'src/create.sql';
一種。 使用以下命令在資源中添加一個create.sql文件:
/*create database at first time*/
create schema IF NOT EXISTS MYDBNAME;
灣 並將屬性“ hbm2ddl.import_files”添加到“ hibernate.cfg.xml””,如下所示:
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.import_files">path/create.sql</property>
other properties
</session-factory>
</hibernate-configuration>
因此,如果數據庫不存在,休眠將創建新數據庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.