1

アプリでhibernate jpaを使用しています。テーブルを自動的に作成できますが、削除することはできません。すでに「hibernate.hbm2ddl.auto」を「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>

リポジトリ.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);
4

1 に答える 1

0

create-drop閉じるとテーブルが削除EntityManagerFactoryされますが、コードでテーブルを閉じることはないようです。

于 2013-02-20T08:55:00.360 に答える