为什么hbm2ddl.auto'update'属性不起作用或者它是如何工作的?

时间:2017-11-08 12:12:59

标签: java hibernate

我是Hibernate的新手,据我所知,当hbm2ddl.auto设置为'update'时,它应该创建一个表,如果它不存在,它应该自动创建一个新列,如果新的'属性'列的标记添加在映射文件中。对? 但每当我试图运行我的类时,它会抛出“表或视图不存在”而不是创建它。我正在使用hibernate v5.10

这是我试图运行的POC示例。提前谢谢。

映射文件

<?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)
....

2 个答案:

答案 0 :(得分:2)

对于仍在搜索答案的任何人,如果您想体验“更新”属性的所有功能,如自动创建表格,自动添加列(两种情况:如果已经不可用),那么请转到5.0.1 .Final版本,它是支持它们的最稳定的版本。

答案 1 :(得分:0)

我认为您需要&#34;创建&#34;,&#34;更新&#34;将只更新现有表,请参阅docs: https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch03.html