2016-02-09 43 views
0

我已經開始學習HQL最近,並試圖創建一個測試應用程序,看看它是如何工作..但是當我試圖運行該項目,我看到重複的列正在創建NULL值它。hibernate自動創建重複列

這裏是我的項目詳情

我有這種結構{ID,insurance_name,invested_amount,investment_date}我已經創建一個表(保險)。

當我運行應用程序時,它創建了兩個帶有空值的附加列{id,insurance_name,invest_amount,investment_date,INSURANCE_AMOUNT,INSURANCE_DATE},我不知道它們從哪裏創建。

休眠配置文件:

<?xml version="1.0"?> 
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 
<session-factory> 
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate4</property> 
<property name="hibernate.connection.username">root</property> 
<property name="hibernate.connection.password">root</property> 
<property name="hibernate.connection.pool_size">10</property> 
<property name="show_sql">true</property> 
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
<property name="hibernate.hbm2ddl.auto">update</property> 
<mapping resource="insurance.hbm.xml"/> 
</session-factory> 

映射文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping> 
<class name="com.roseindia.app.HQL.Insurance" table="INSURANCE"> 
    <id name="id" type="long" column="ID"> 
     <generator class="increment"></generator> 
    </id> 
    <property name="insuranceName"> 
     <column name="INSURANCE_NAME"></column> 
    </property> 
    <property name="investmentAmount"> 
     <column name="INSURANCE_AMOUNT"></column> 
    </property> 
    <property name="investmentDate"> 
     <column name="INSURANCE_DATE"></column> 
    </property> 
</class> 
</hibernate-mapping> 

POJO類:

public class Insurance { 

private Long id; 
private String insuranceName; 
private Integer investmentAmount; 
private Date investmentDate; 

public Long getId() { 
    return id; 
} 
public void setId(Long id) { 
    this.id = id; 
} 
public String getInsuranceName() { 
    return insuranceName; 
} 
public void setInsuranceName(String insuranceName) { 
    this.insuranceName = insuranceName; 
} 
public Integer getInvestmentAmount() { 
    return investmentAmount; 
} 
public void setInvestmentAmount(Integer investmentAmount) { 
    this.investmentAmount = investmentAmount; 
} 
public Date getInvestmentDate() { 
    return investmentDate; 
} 
public void setInvestmentDate(Date investmentDate) { 
    this.investmentDate = investmentDate; 
} 
} 

主類:

public class SelectClauseExample { 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    Session session =null; 

    try{ 

     SessionFactory sessionFactory =new Configuration().configure().buildSessionFactory(); 
     session= sessionFactory.openSession(); 

     String SQL_QUERY="select insurance.id, insurance.insuranceName,insurance.investmentAmount,insurance.investmentDate from Insurance insurance"; 

     Query query = session.createQuery(SQL_QUERY); 

     for (java.util.Iterator it = query.iterate(); it.hasNext();){ 
      System.out.print("Inside for"); 
      Object[] row = (Object[]) it.next(); 
      System.out.println("ID: "+row[0]); 
      System.out.println("Name: "+row[1]); 
      System.out.println("Amount: "+row[2]); 
      System.out.println("Date: "+row[3]); 
     } 


    }catch(Exception e){ 

    }finally{ 
     session.flush(); 
     session.close(); 

    } 

} 

} 

回答

0

你shoudd從您的配置文件跳過酒店hibernate.hbm2ddl.auto(這意味着默認值,這意味着沒有驗證,更新,創建或忽略了此設置時落下發生),或使用validate,如果你不希望在任何更新你的數據庫

請參閱Hibernate hbm2ddl.auto, possible values and what they do - any official explanation?知道 'hibernate.hbm2ddl.auto'

+0

謝謝..它的工作..也有問題與我的映射文件..我映射不正確的列名映射文件中。 – user2954417

0

嘗試的可能值來代替這個:

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

附:

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

在這種模式下,你會不會在你的模式中的任何改變,因爲你在你的文件Mapping file:

 <property name="investmentAmount"> 
    <column name="INSURANCE_AMOUNT"></column> 
</property> 
<property name="investmentDate"> 
    <column name="INSURANCE_DATE"></column> 

當您運行upplication yoyr模式每次都會有被改變。