2013-02-19 46 views
1

我在我的应用程序中使用hibernate jpa,它可以自动创建表,但不能删除它,我已经将“hibernate.hbm2ddl.auto”设置为“创建 - 删除”。这里是我的设置:hibernate jpa不删表

<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); 

回答

0

随着create-drop表被删除当您关闭EntityManagerFactory但似乎你永远在你的代码中关闭它。