這段時間老總要我修改公司原先的人做的項目,是采用spring+struts+hibernate+ajax做得,要求將數據庫改為oracle(以前的是mysql)。我對hibernate,無賴老總要求,只得邊學邊改。通過使用myeclipse來生成的映射文件,能按照數據庫表來生成映射文件。但這樣生成的映射文件確保一定能用。
如,有以下映射文件
<hibernate-mapping>
    
<class name="com.hibernate.temp.News" table="NEWS" schema="PIRATESTUDIO">
        
<id name="id" type="java.lang.Long">
            
<column name="ID" precision="10" scale="0" />
            
<generator class="assigned" />
        
</id>
        
<property name="NType" type="java.lang.String">
            
<column name="N_TYPE" length="10" not-null="true" />
        
</property>
        
<property name="NTitle" type="java.lang.String">
            
<column name="N_TITLE" length="50" not-null="true" />
        
</property>
        
<property name="NContent" type="java.lang.String">
            
<column name="N_CONTENT" not-null="true" />
        
</property>
        
<property name="NIp" type="java.lang.String">
            
<column name="N_IP" length="20" not-null="true" />
        
</property>
        
<property name="NTime" type="java.util.Date">
            
<column name="N_TIME" length="7" not-null="true" />
        
</property>
        
<property name="NRRate" type="java.lang.Long">
            
<column name="N_R_RATE" precision="10" scale="0" not-null="true" />
        
</property>
        
<property name="NTypeT" type="java.lang.Long">
            
<column name="N_TYPE_T" precision="10" scale="0" not-null="true" />
        
</property>
        
<property name="NComm" type="java.lang.Long">
            
<column name="N_COMM" precision="10" scale="0" not-null="true" />
        
</property>
        
<property name="NIsedit" type="java.lang.Long">
            
<column name="N_ISEDIT" precision="10" scale="0" not-null="true" />
        
</property>
    
</class>
當我在使用hibernate的getHiberbnateTemplate().getDelete(entity)時,就會報以下錯誤
javax.servlet.ServletException: org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: com.GameServer.Entity.News.NTitle; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: com.GameServer.Entity.News.xxxx(xxxx是映射未見中的column的name)
這是因為我在映射文件中使用not-null="true" 這使用delete()方法是系統會根據not-null來檢查相應的值。而我的entity中只用id是不為null,其余是null。因此報錯。
通過這個可以得出,hiberbnate的映射文件最好自己來寫,這樣才能達到靈活性。