hibernate5.2.4:每次运行hibernate都会创建表

我导入的jar包是required里所有和jdbc
hibernate.cfg.xml
注意hbm2ddl.auto属性

<?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="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  <property name="hibernate.connection.password">scott</property>
  <property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>
  <property name="hibernate.connection.username">scott</property>
  <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.format_sql">true</property>
  <property name="hibernate.hbm2ddl.auto">update</property>
  <mapping resource="entity/Emp.hbm.xml"/>
  <mapping resource="entity/Dept.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

emp映射文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
 <class name="entity.Emp" table="EMP">
  <id name="empno" type="java.lang.Integer"/>
  <property name="ename" type="java.lang.String"/>
  <property name="job" type="java.lang.String"/>
  <property name="mgr" type="java.lang.Integer"/>
  <property name="hiredate" type="java.sql.Date"/>
  <property  name="sal" type="java.lang.Double"/>
  <property  name="comm" type="java.lang.Double"/>
  <many-to-one class="entity.Dept" column="deptno" name="d"/>
 </class>
</hibernate-mapping> 

dept映射文件

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
 <class name="entity.Dept" table="DEPT">
  <id name="deptno" type="java.lang.Integer"/>
  <property generated="never" lazy="false" name="dname" type="java.lang.String"/>
  <property generated="never" lazy="false" name="loc" type="java.lang.String"/>
  <list name="l" table="EMP">
   <key column="deptno"/>
   <list-index column="deptNo"/>
   <one-to-many class="entity.Emp"/>
  </list>
    </class>
    </hibernate-mapping>

java测试类

 package test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import entity.Emp;

public class Test {
    public static void main(String[] args) {
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();

        SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        Emp e = new Emp();
        e.setEmpno(652);
        session.save(e);
        session.getTransaction().commit();

    }
}

控制台信息

十一月 21, 2016 8:56:30 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.4.Final}
十一月 21, 2016 8:56:30 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
十一月 21, 2016 8:56:30 下午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
十一月 21, 2016 8:56:31 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
十一月 21, 2016 8:56:31 下午 org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-mapping. Use namespace http://www.hibernate.org/dtd/hibernate-mapping instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
十一月 21, 2016 8:56:32 下午 org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-mapping. Use namespace http://www.hibernate.org/dtd/hibernate-mapping instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
十一月 21, 2016 8:56:32 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
十一月 21, 2016 8:56:32 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thava:191)
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:523)
    at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:193)
    at oracle.jdbc.driver.T4CStatement.executeForRows(T4CStatement.java:999)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1315)
    at oracle.jdbc.driver.OracleStatement.executeInternal(OracleStatement.java:1890)
    at oracle.jdbc.driver.OracleStatement.execute(OracleStatement.java:1855)
    at oracle.jdbc.driver.OracleStatementWrapper.execute(OracleStatementWrapper.java:304)
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54) 

如果我把hbm2ddl属性换成create也会造成这种冲突,请问大神怎么破

3个回答

using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thava:191)首先确定这个驱动包是否正确,然后在tomcat的lib包下面放入你的oracle数据驱动包jar包,希望能解决

qq_15731659
qq_15731659 我的驱动包是ojdbc6应该没问题吧
3 年多之前 回复

总算是知道怎么做了,具体原因不知道。如果有碰到类似问题的朋友。可以在主配置文件 配置一下 default_schema 值为用户名。如果有外键冲突问题,可以在多对一加个foreign-key属性,值为约束名。当然最简单的方法就是把生成策略删除。以上是针对oracle数据库。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问