1. org/hibernate/criterion/Criterion
工程布署的時候沒有加入hibernate3的jar包,這個jar不是默認加入的,要在myeclipse -> add hibernate capabilities... ->選擇copy checked libraries to project folder and add to build-path否則工程布署的時候無論如何是不會有hibernate的包的。
2. org.hibernate.id.IdentifierGenerationException
當出現(xiàn)org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():異常時,一般是因為<id>元素配置不正確,<id>元素缺少其子元素<generator></generator>的配置引起。
解決方案:<id>元素映射了相應數(shù)據(jù)庫表的主鍵字段,對其子元素<generator class="">,其中class的取值可以為increment、identity、sequence、hilo、native……等,更多的可參考hibernate參考文檔,一般取其值為native 。
generator class="assigned“而類型type="java.lang.Integer”如果主鍵時自動增長的Int型,把assigned改成自動增長的Identity,assigned是自動增長的varchar型的。
3. org.hibernate.PropertyValueException
數(shù)據(jù)庫的表里字段設有not null,執(zhí)行dao.delete(op); 時出現(xiàn)異常:
org.hibernate.PropertyValueException: not-null property references a null or transient value: com.easyfile.util. Operators. operatorid
在OperatorsDAO.java中private static Logger logger = Logger.getLogger(AdminAction.class);這句并沒有顯式地自動加載Id過來,還是需要dao.getSession().load(op,op.getOperatorid());來加載id。完整的代碼如下:
String operatorid = request.getParameter("delOperatorid");
OperatorsDAO dao=new OperatorsDAO();
Transaction tran=dao.getSession().beginTransaction();
Operators op = new Operators();
op.setOperatorid(Integer.parseInt(operatorid));
dao.getSession().load(op,op.getOperatorid());
dao.delete(op);
tran.commit();
dao.getSession().close();
4. null id in entry (don't flush the Session after an exception occurs)
產生該異常信息有2種可能:
Ø 我們沒有為數(shù)據(jù)中的非空字段設置值。如果我們在通過Hibernate增加一條記錄的時候我們必須顯式的通過setXxx方法為該屬性賦值(/默認值)。因為在保存之前Hibernate會檢查該非空字段對應的實體屬性是否為空。如果不想顯式賦值的話,我們可以通過xxx.hbm.xml配置文件來實現(xiàn),也就是在配置文件中給出該字段的默認值。或者在你的類中設置默認值就行了。注意的是rename數(shù)據(jù)庫保留字段。
Ø 在hibernate的配置文件中,有的元素有unique屬性的配置,它在數(shù)據(jù)添加時并不起任何作用,只在從hbm文件生成ddl語句時才有作用,并不會在運行時校驗數(shù)據(jù)。防止數(shù)據(jù)重復添加,要么在數(shù)據(jù)庫上建立唯一索引(數(shù)據(jù)庫保證),要么在插入時提前校驗(人為保證)。當然,多數(shù)情況下是兩者結合。
5. org.hibernate.hql.ast.QuerySyntaxException
Ø 持久類寫錯了,要不就是寫成了數(shù)據(jù)庫表名
Ø hibernate3.0不支持select中嵌套查詢,據(jù)說from中也不行,只支持where中嵌套查詢,好像3.1支持了select中嵌套
Ø sql語句中字段是用了保留關鍵字
posted on 2009-01-21 20:01
飛翔天使 閱讀(5068)
評論(1) 編輯 收藏 所屬分類:
Hibernate