org.hibernate.HibernateException: Wrong column type

來源:互聯網
上載者:User

標籤:索引   .net   creat   arc   property   sql   cas   .so   factory   

        這個問題一般出現在我們使用定長的字串作為主鍵(其它欄位也可能)的時候,如資料庫中的ID為char(16)。雖然很多資料上都說不推薦這樣做,但實際上我們在做很多小case的時候自己為了方便也顧不得那麼多,隨心所欲地設計。其實我們就用INT的主鍵,只是對你原有的ID(char(16))增加一個Unique Check或者是像在MySQL中增加一個Unique索引又費得了多少事呢。

        如果使用char()做為主鍵時出現如題錯誤,其很可能的原因在於你的hibernate.cfg.xml檔案中的關於Hibernate映射到資料定義語言 (Data Definition Language)(DDL)的配置

 

[html] view plain copy 
  1. <property name="hbm2ddl.auto">validate</property>  

        當hbm2ddl設定為validate,每次載入hibernate時,驗證建立資料庫表結構,只會和資料庫中的表進行比較,不會建立新表,但是會插入新值。validate的具體內部實現我不清楚,但我想正是因為每次驗證比較導致了如題問題的出現。資料庫裡欄位類型為char(),而你的對象屬性為java.lang.String,出現了錯誤的列類型。

       這樣的錯誤並不是經常出現,原因在於我們配置hibernate.cfg.xml檔案的時候一般不配置hbm2ddl這一項,即使用預設值“update”,而且在開發或學習的過程中我們通常會配置為“create”,也就很難遇到這樣的錯誤。到這裡解決辦法已經很明確了,即更改你的hbm2ddl配置。

       下面給出一個使用char()做主鍵的配置執行個體(源自: Dashboard(Hibernate入門)):

       在mysql中新增一個HibernateTest資料庫,並建立USER表格

 

[sql] view plain copy 
  1. CREATE TABLE USER (  
  2.     user_id CHAR(32) NOT NULL PRIMARY KEY,  
  3.     name VARCHAR(16) NOT NULL,  
  4.     sex CHAR(1),  
  5.     age INT  
  6. );  

       

 

      Java類User.java

 

[java] view plain copy 
  1. package onlyfun.caterpillar;  
  2.   
  3. public class User {  
  4.     private String id;  
  5.     private String name;  
  6.     private char sex;  
  7.     private int age;  
  8.   
  9.     public int getAge() {  
  10.         return age;  
  11.     }  
  12.   
  13.     public String getId() {  
  14.         return id;  
  15.     }  
  16.   
  17.     public String getName() {  
  18.         return name;  
  19.     }  
  20.   
  21.     public char getSex() {  
  22.         return sex;  
  23.     }  
  24.   
  25.     public void setAge(int i) {  
  26.         age = i;  
  27.     }  
  28.   
  29.     public void setId(String string) {  
  30.         id = string;  
  31.     }  
  32.   
  33.     public void setName(String string) {  
  34.         name = string;  
  35.     }  
  36.   
  37.     public void setSex(char c) {  
  38.         sex = c;  
  39.     }  
  40. }  


       User.hbm.xml檔案配置

 

 

[html] view plain copy 
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping  
  3.     PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"  
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">  
  5.   
  6. <hibernate-mapping>  
  7.   
  8.     <class name="onlyfun.caterpillar.User" table="USER">  
  9.   
  10.         <id name="id" type="string" unsaved-value="null">  
  11.             <column name="user_id" sql-type="char(32)" />  
  12.             <generator class="uuid.hex"/>  
  13.         </id>  
  14.   
  15.         <property name="name" type="string" not-null="true">  
  16.             <column name="name" length="16" not-null="true"/>  
  17.         </property>  
  18.   
  19.         <property name="sex" type="char"/>  
  20.   
  21.         <property name="age" type="int"/>  
  22.   
  23.     </class>  
  24.   
  25. </hibernate-mapping>  


       hibernate.cfg.xml檔案配置

 

 

[html] view plain copy 
  1. <hibernate-configuration>  
  2.   
  3.     <session-factory>  
  4.   
  5.         <property name="show_sql">true</property>  
  6.           
  7.         <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>  
  8.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  9.   
  10.         <property name="connection.url">jdbc:mysql://localhost/HibernateTest</property>  
  11.         <property name="connection.username">caterpillar</property>  
  12.       
  13.         <property name="connection.password">123456</property>  
  14.   
  15.         <mapping resource="User.hbm.xml"/>  
  16.   
  17.     </session-factory>  
  18.   
  19. </hibernate-configuration>  


      上面只是個簡單的配置執行個體,主要在於展示User的char(32)類型的ID如何配置。

 

      剛接觸Hibernate,有些簡單的問題也會搞得你暈頭轉向,以此作為自己的學習筆記,歡迎指導!

org.hibernate.HibernateException: Wrong column type

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.