2013-10-23 5 views
0

복제 된 경우 사과드립니다. 내 질문에 대한 답을 찾을 수 없었습니다.자동 구성표 생성을 중지하려면 어떻게해야합니까?

난 그냥 최대 절전 모드를 시작합니다. 프로그램이 자동으로 오래된 shceme을 삭제하고 실행될 때마다 새로운 문제가 발생할 때마다 문제가 발생합니다. 예를 들어 데이터베이스에 레코드를 추가하려는 경우 스키마가 다시 만들어지기 때문에 레코드를 삭제할 수 없으므로 레코드가 삭제됩니다.

<?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 22-Oct-2013 1:39:31 PM by Hibernate Tools 3.4.0.CR1 --> 
<hibernate-mapping> 
    <class name="net.ys.hibernate.Equip" table="EQUIP"> 
     <id name="id" type="int"> 
      <column name="ID" /> 
      <generator class="native" /> 
     </id> 
     <property name="dis" type="java.lang.String" column="dis" /> 

     <property name="ref" type="java.lang.String" column="ref" /> 

     <property name="type" type="java.lang.String" column="type" /> 

    </class> 
</hibernate-mapping> 

있는 hibernate.cfg.xml 구성 파일 :

은 여기 내 hbm.xml 매핑 파일입니다.

<?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> 

     <!-- hibernate dialect --> 

     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="hibernate.connection.password">pw</property> 

     <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/explorer_DB?useUnicode=true&amp;characterEncoding=GBK</property> 
     <property name="hibernate.connection.username">username</property> 
     <property name="hibernate.default_schema">explorer_DB</property> 
     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 

     <!-- Automatic schema creation(begin) --> 
     <property name="hibernate.hbm2ddl.auto">create</property> 
     <!-- Simple memory-only cache --> 
     <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property> 
     <!-- Enable Hibernate's automatic session context management --> 
     <property name="current_session_context_class">thread</property> 

     <!-- mapping files with external dependencies --> 
     <mapping resource="net/ys/hibernate/Equip.hbm.xml"/> 


    </session-factory> 
</hibernate-configuration> 

그리고 addEquip 방법 :

public Integer addEquip(String dis, String ref, String type){ 
     Session session = sessionFactory.openSession(); 
     currentSession = sessionFactory.getCurrentSession(); 
     Transaction tx = null; 
     Integer equipID = null; 

     try { 
      tx = currentSession.beginTransaction(); //start a transaction 

      Equip equip = new Equip(dis,ref,type); 
      equipID = (Integer)currentSession.save(equip); 
      tx.commit(); 
     } catch (HibernateException e) { 
      if(tx!=null) tx.rollback(); 
      e.printStackTrace(); 
     } finally { 
      session.close(); 
     } 

     return equipID; 

    } 
} 

누군가가 나에게이 문제를 해결하는 데 도움이 될 수

? 난 단지 getCurrentSession()을 사용하는 방법을 이해하지 못한다. 아마도 나는이 시점에서 잘못되었다. 제게 getCurrentSession()을 호출 할 때 최대 절전 모드가 어떻게 작동하는지 설명해 주시겠습니까? 정말 감사. 변화는 데이터베이스 사용을 요구하지 않는 경우

<property name="hibernate.hbm2ddl.auto">none</property> 

hibernate.cfg.xml 파일

현재 구성

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

당신에게

+0

당신이 세션 공장마다 작성하는 것에

<property name="hibernate.hbm2ddl.auto">validate</property> 

더, 당신은을 확인할 수 있습니다? – Admit

+0

@Admit 안녕하세요, addEquip()에서 먼저 openSession()을 열고 CurrentSession을 가져 오십시오. 그런 다음 currentSession.beginTransaction()을 호출하면 트랜잭션이 시작됩니다. 그러나 으로 변경하면 아무런 문제도 해결되지 않습니다. – Eric

+0

스키마 생성이 전혀 필요하지 않은 경우 스키마를 제거 할 수도 있습니다 (none 값과 같습니다). 'sessionFactory'를 생성하는 장소를 살펴보십시오. 왜냐하면 모든 요청에 ​​대해 새로운 팩토리를 생성하는 것이 효율적이지 않은 것 같고 문제의 근원 인 것 같습니다. – Admit

답변

1

변경이 부동산에 너무 감사드립니다. 이 참조하시기 바랍니다이 링크

Hibernate hbm2ddl.auto possible values and what they do?

+0

안녕 ByteCode, 고마워, 작동합니다. 그러나 내가 처음 계획을 가지고 있지 않을 때 그것은 효과가 없습니다. 스키마가없는 경우 데이터베이스를 확인할 수있는 방법이 있습니까? 자동으로 스키마가 만들어집니다. 우리가 가지고 있다면 자동 생성없이 추가, 삭제 또는 업데이트하십시오. – Eric

관련 문제