为什么hbm2ddl.auto'update'属性不起作用或它如何起作用? [英] Why hbm2ddl.auto 'update' property is not working or how does it works?
问题描述
我是Hibernate的新手,据我所知,当hbm2ddl.auto设置为'update'时,它应该创建一个表(如果它不存在),它应该自动创建一个新列,如果新的'property'列的标记已添加到映射文件中.正确的? 但是,每当我尝试运行我的课程时,它都会抛出表或视图不存在"而不是创建它.我正在使用休眠v5.10
I am new to Hibernate, As per my knowledge, when hbm2ddl.auto is set to 'update', it should create a table if its doesn't exist, it should create a new column automatically if the new 'property' tag for column is added in mapping file. Right? But whenever i am trying to run my class, it is throwing "Table or view doesn't exist" instead of creating it. I am using hibernate v5.10
这是我要运行的我的POC示例.预先感谢.
Here's my POC example i am trying to run. Thanks in advance.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.nt.domain.Customer" table="CUSTOMER">
<id name="custNo" length="10" type="int" column="CUSTNO"/>
<property name="customerName" length="20" type="string" column="CUSTNAME"/>
<property name="billAmt" length="10" type="int" column="BILLAMT"/>
</class>
</hibernate-mapping>
配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-// Hibernate/ Hibernate Configuration DTD 3.0 //EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:sys</property>
<property name="connection.username">Asif123</property>
<property name="connection.password">Asif123</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<property name="dialect">org.hibernate.dialect.Oracle12cDialect</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/nt/domain/customer.hbm.xml"/>
</session-factory>
package com.nt.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.nt.domain.Customer;
public class UpdateTest {
public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory();
Session ses= factory.openSession();
Transaction tx= ses.beginTransaction();
Customer customer = new Customer();
customer.setCustNo(101);
customer.setCustomerName("Asif");
customer.setBillAmt(1245);
ses.save(customer);
tx.commit();
ses.close();
factory.close();
}
}
客户
package com.nt.domain;
public class Customer {
int custNo;
String customerName;
int billAmt;
//All getters and setters
}
异常
Caused by: java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1059)
....
推荐答案
对于仍在寻找答案的任何人,如果您想体验更新"属性的所有功能,例如自动创建表,自动添加列(两种情况, :如果尚不可用),则选择5.0.1.Final版本,它是支持它们的最稳定的版本.
For anyone still searching for an answer, If you want to experience all the functionalities of "update" property like auto table creation, auto adding of columns (both case : if already not available), then go for the 5.0.1.Final version, it is the most stable version supporting them.
这篇关于为什么hbm2ddl.auto'update'属性不起作用或它如何起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!