hibernate jpa不会删除表

时间:2013-02-19 21:30:58

标签: hibernate jpa

我在我的应用程序中使用hibernate jpa,它可以自动创建表但不删除它,我已经设置了" hibernate.hbm2ddl.auto" to" create-drop"。这是我的设置:

  <persistence-unit name="jobs-postgres">
    <properties>
      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
      <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
      <property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />
      <property name="hibernate.hbm2ddl.auto" value="create-drop" />

      <!-- Connection provider properties. -->
      <property name="hibernate.c3p0.max_size" value="100" />
      <property name="hibernate.c3p0.min_size" value="1" />
      <property name="hibernate.c3p0.acquire_increment" value="1" />
      <property name="hibernate.c3p0.idle_test_period" value="60" />
      <property name="hibernate.c3p0.max_statements" value="0" />
      <property name="hibernate.c3p0.timeout" value="30" />

      <!-- Miscellaneous Hibernate properties. --> 
      <property name="hibernate.max_fetch_depth" value="3" />
      <property name="hibernate.cache.use_query_cache" value="false" />
      <property name="hibernate.cache.use_second_level_cache" value="false" />
      <property name="hibernate.search.autoregister_listeners" value="false" />
      <property name="hibernate.jdbc.batch_size" value="1000"/>
      <property name="hibernate.generate_statistics" value="false" />
      <property name="hibernate.show_sql" value="false" />        
    </properties>    
  </persistence-unit>

repository.java

public class JPATagsRepository {
    private static final Logger LOGGER = LoggerFactory.getLogger(JPATagsRepository.class);

    private final EntityManagerFactory _emf;

    public JPATagsRepository(EntityManagerFactory emf) {
        if (emf == null) {
            throw new IllegalArgumentException("Entity manager cannot be null.");
        }
        this._emf = emf;
    }

public void store(Set<String> tagStr, int type) {
    if (tagStr.size() == 0) {
        throw new IllegalArgumentException("Tags are empty.");
    }

    EntityManager em = null;
    EntityTransaction tx = null;

    try {
        em = _emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();

        int i = 0;
        for (String s : tagStr) {
            Tag tag = new Tag(s, type);

            if (getByName(tag.getName()) == null) {
                em.persist(tag);
            }

            if (++i % 1000 == 0) {
                em.flush();
                em.clear();
            }
        }

        tx.commit();
    }
    finally {
        if (tx != null && tx.isActive()) {
            tx.rollback();
        }
        if (em != null) {
            em.close();
            em = null;
        }
    }
}
}

app.java

Properties config = new Properties();
FileInputStream in = new FileInputStream(DEFAULT_OP_CONFIG_LOCATION);
config.load(in);
in.close();

Map<String, String> properties = Maps.newHashMap();
properties.put("javax.persistence.jdbc.url", config.getProperty("db_url"));
properties.put("javax.persistence.jdbc.user", config.getProperty("db_user"));
properties.put("javax.persistence.jdbc.password", config.getProperty("db_password"));

EntityManagerFactory emf = Persistence.createEntityManagerFactory("jobs-postgres", properties);         

JPATagsRepository tagRepository = new JPATagsRepository(emf);

1 个答案:

答案 0 :(得分:0)

使用create-drop时,关闭EntityManagerFactory时会删除这些表格,但您似乎永远不会在代码中将其关闭。

相关问题