這段時(shí)間老總要我修改公司原先的人做的項(xiàng)目,是采用spring+struts+hibernate+ajax做得,要求將數(shù)據(jù)庫(kù)改為oracle(以前的是mysql)。我對(duì)hibernate,無(wú)賴?yán)峡傄螅坏眠厡W(xué)邊改。通過(guò)使用myeclipse來(lái)生成的映射文件,能按照數(shù)據(jù)庫(kù)表來(lái)生成映射文件。但這樣生成的映射文件確保一定能用。
如,有以下映射文件
<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>
當(dāng)我在使用hibernate的getHiberbnateTemplate().getDelete(entity)時(shí),就會(huì)報(bào)以下錯(cuò)誤
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)
這是因?yàn)槲以谟成湮募惺褂胣ot-null="true" 這使用delete()方法是系統(tǒng)會(huì)根據(jù)not-null來(lái)檢查相應(yīng)的值。而我的entity中只用id是不為null,其余是null。因此報(bào)錯(cuò)。
通過(guò)這個(gè)可以得出,hiberbnate的映射文件最好自己來(lái)寫,這樣才能達(dá)到靈活性。