Hibernate <property name =“hbm2ddl.auto”> create </property> - 如果存在挂起,则删除表

时间:2014-06-12 04:32:08

标签: hibernate

我正在编写一个带有hbm2ddl.create属性的示例hibernate程序,其中包含&#34; Create&#34; /&#34; create-drop&#34; hibernate配置文件中的值。我认为程序的每次执行都会丢弃表并创建一个新表。但它没有按预期工作。当我第一次跑步时,它会很好地创建表格。

但是当我再次运行相同的程序时,Drop表hbm2ddl架构导出tablename如果存在sql将永远挂起。对于每次运行,我都被迫重启数据库服务器。 (在这种情况下是Mysql。)这是因为某些交易问题吗?

以下是示例程序

配置文件: -

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory name="">
  <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/localdb</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">root</property>
  <property name="hibernate.current_session_context_class">thread</property>
  <property name="show_sql">True</property>
 <!-- <property name="hbm2ddl.auto">create</property>(Tried create and create-drop.Both behaves the same way/)-->
     <property name="hbm2ddl.auto">create-drop</property>
  <mapping resource="com/test/manytomany/domain/TemporalTest.hbm.xml"/>
<!--    <mapping resource="com/test/manytomany/domain/Event.hbm.xml"/>
        <mapping resource="com/test/manytomany/domain/Person.hbm.xml"/>
 --> </session-factory>
</hibernate-configuration>

HBM文件: -

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 11, 2014 7:09:00 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.test.manytomany.domain.TemporalTest" table="TEMPORAL_TEST">
        <id name="temporalID" type="int">
            <column name="TEMPORAL_ID" />
        </id>
        <property name="temporalDate" >
            <column name="TEMPORAL_DATE" />
        </property>
    </class>
</hibernate-mapping>

Pojo类: -

package com.test.manytomany.domain;

import java.sql.Time;
import java.util.Date;

import javax.persistence.Temporal;
import javax.persistence.TemporalType;

public class TemporalTest {


    private int temporalID;
    @Temporal(TemporalType.TIME)
    private Time temporalDate;


    public TemporalTest(){}
    /**
     * @return the temporalID
     */
    public int getTemporalID() {
        return temporalID;
    }
    /**
     * @param temporalID the temporalID to set
     */
    public void setTemporalID(int temporalID) {
        this.temporalID = temporalID;
    }

    /**
     * @return the temporalDate
     */
    public Time getTemporalDate() {
        return temporalDate;
    }
    /**
     * @param temporalDate the temporalDate to set
     */
    public void setTemporalDate(Time temporalDate) {
        this.temporalDate = temporalDate;
    }

}

Hibernate Main: -

package com.test.manytomany.main;

import java.sql.Time;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.test.manytomany.domain.TemporalTest;

public class TemporalTestMain {

    public static void main(String args[]){

        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        System.out.println(" Transaction Started " );
        TemporalTest temporalTest = new TemporalTest();
        temporalTest.setTemporalID(1);
        Time d1 = new Time(55,50,50);
        System.out.println(d1);
        temporalTest.setTemporalDate(d1);
        session.save(temporalTest);

        d1 = new Time(50,30,40);
        System.out.println(d1);
        temporalTest = new TemporalTest();
        temporalTest.setTemporalID(2);
        temporalTest.setTemporalDate(d1);
        session.save(temporalTest);
        session.getTransaction().commit();

        session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        Criteria cr = session.createCriteria(TemporalTest.class);
        List list = cr.list();
        Iterator iter = list.iterator();
        while(iter.hasNext()){
            temporalTest = (TemporalTest) iter.next();
            System.out.println(temporalTest.getTemporalID());
            System.out.println(temporalTest.getTemporalDate());
        }
        session.close();
    }
}

以下SQL永远挂起: -

2014年6月11日9:30:27 org.hibernate.tool.hbm2ddl.SchemaExport执行 信息:HHH000227:运行hbm2ddl架构导出 Hibernate:drop table如果存在TEMPORAL_TEST

2 个答案:

答案 0 :(得分:0)

您应该使用:

<property name="hibernate.hbm2ddl.auto">create-drop</property>

答案 1 :(得分:0)

我遇到了类似的问题,但我错过了session.close()方法。一旦我添加了session.close(),我就可以重复运行相同的程序而没有任何问题。