no bean specialed.
出錯了,jsp頁面報錯。
緣由是html:select 標籤 中 option bean is null/.html
Set不能加同一實體
在保存數據的時候循環添加一PO數據到Set,竟然最後Set的size()爲1。各實體設置不一樣數據啊,只是沒Id.
緣由是這個PO之前本身實現了Comparable .只比較Id,java
HibernateSystemException: SQL insert,update or delete failed
hibernate級聯保存或更新時可能會出這個錯誤
緣由是因爲子實體的主鍵設置了不符合保存更新的數據.mysql
No persister for錯誤
這個錯誤說的是no persister for java.lang.Integer;
映射文件未添加確定不是這個問題了。
那只有映射文件出錯。
鎖定了關聯的id;
PO裏面寫的關聯ID,而建表看不出問題。並且映射文件裏面基本上看不出來。
出現No persister for錯誤後通常是3個狀況:
1hbm.xml文件特殊類未制定類型。
2cfg.xml文件未添加類的hbm.xml文件名。
3PO和 hbm類映射不匹配。sql
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed
hibernate最多見錯誤.
Lazy爲true
session關閉後調用延遲加載屬性.數據庫
在session關閉以前若是調用該屬性,會加載.
或者是使用Hibernate.Initalize();tomcat
BigDecimal對象屬性映射出錯
這個問題是不當心形成的.
屬性映射應該加上 length="2"指定2位小數,或者幾位小數,小數位數在0-19之間session
property value with CGLIB 錯誤
比較常見的hibernate錯誤了.
當PO屬性爲原始類型(int,double...)時,數據庫紀錄爲null,調出PO時因爲這些屬性必須有值而出現錯誤.app
把屬性值設置爲對象屬性解決.dom
hibernate出錯
在configure.buildSessionFactory()時java.nullpointException.
在eclipse裏沒問題,但部署在tomcat裏就出問題了,
是因爲包衝突了。
還有個可能就是configure(path),path不對。eclipse
是因爲eclipse的class loader和tomcat不一樣,在tomcat裏,不能有兩個同樣一塊兒加載。
eclipse裏倒是指定jar的。
常見錯誤:
Caused by: org.dom4j.DocumentException: Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence.
若是出現這行錯誤說明你的xml配置文件有不規範的字符,檢查下。
net.sf.hibernate.MappingException: Error reading resource: hibernate/Hello_Bean.hbm.xml
若是出現這行錯誤說明你的hibernate的XML配置文件有錯
net.sf.hibernate.MappingException: Resource: hibernate/Hello_Bean.hbm.xml not found
若是出現這行錯誤說明hibernate的XML配置文件沒有找到,你應該把XML文件放在與你的類文件同個目錄下,本文中是放在hibernate\classes\hibernate\目錄下,也就是跟Hello_Bean.class類文件一塊兒。
net.sf.hibernate.PropertyNotFoundException: Could not find a setter for property name in class hibernate.Hello_Bean
若是出現這行錯誤說明你的xml文件裏設置的字段名name的值與Hello_Bean.java類裏的getXXX或setXXX方法不一致。
net.sf.hibernate.HibernateException: JDBC Driver class not found: org.gjt.mm.mysql.Driver
若是出現這行錯誤說明你的MYSQL驅動沒有加進JB庫裏或者不在CLASSPATH裏。
調試出現 net.sf.hibernate.MappingException 頗有多是因爲
類庫文件沒有找到,類庫的版本不一樣,他的名字會不一樣 要從新配置 setenv.bat
類庫的路徑。
本身遇到的hibernate常見錯誤
1>
錯誤顯示: net.sf.hibernate.PropertyValueException: not-null property references a null or transient value: com.Order.customer
部分原文件:( customer 和 order 類關係:一對多關聯)
Order.hbm.xml …………… < many-to-one name = "customer" column = "CUSTOMER_ID" class = "com.Customer" not-null = "true" cascade = "save-update" /> 執行文件:
………
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
// Create some data and persist it
tx = session.beginTransaction();
Customer customer=new Customer();
customer.setName("Jack");
Order order=new Order();
order.setOrderNumber("Jack_Order001");
session.save(customer);
session.save(order);
tx.commit();
緣由分析:由於在執行代碼中,沒有將 customer 和 order 類一對多關聯起來,若單獨持久化兩個類: session.save(customer);session.save(order); 則在保存 order 的時候,因爲 CUSTOMER_ID是與 customer類外鍵,所以沒法讀取 customer_id, 而在 order.hbm.xml 中指定其不爲空,則產生了以上錯誤。
問題解決: not-null = "true" 改成:not-null="false" 雖然程序無問題,但order表 CUSTOMER_ID爲空,不符合邏輯。應該將指定其一對多的關聯。 order.setCustomer(customer); customer.getOrders().add(order); 2〉 錯誤顯示: RROR SessionImpl:2400 - Could not synchronize database state with session net.sf.hibernate.exception.GenericJDBCException: could not delete collection: [com.Customer.orders#2]
部分原文件:
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Customer customer=(Customer)session.load(Customer.class,new Long(3));
session.delete(customer);
tx.commit();
緣由分析:由於 cascade默認值爲 none,因此當刪除customer時,不會自動刪除與其關聯的order對象。 問題解決:添加語句 cascade = "delete" 3>
錯誤顯示:
17:24:34,992 ERROR JDBCExceptionReporter:58 - [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]在關鍵字 'ORDER' 附近有語法錯誤。 17:24:34,992 WARN JDBCExceptionReporter:57 - SQL Error: 156, SQLState: HY000 17:24:35,002 ERROR JDBCExceptionReporter:58 - [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]在關鍵字 'ORDER' 附近有語法錯誤。 17:24:35,022 WARN JDBCExceptionReporter:34 - SQL Warning: 0, SQLState: net.sf.hibernate.exception.GenericJDBCException : could not initialize collection: [com.Customer.orders#2]
部分原文件: order.hbm.xml
< hibernate-mapping > < class name = "com.Order" table = "ORDER" > < id name = "id" type = "long" column = "ID" > < generator class = "increment" /> </ id > < property name = "orderNumber" type = "string" > < column name = "ORDER_NUMBER" length = "15" /> </ property > < many-to-one name = "customer" column = "CUSTOMER_ID" class = "com.Customer" outer-join = "true" />
緣由分析:由於 order 表在 SQL 2000 數據庫中已經定義了,若是用戶在定義了 order 表,而且程序對該表進行鏈接等操做就會出錯
問題解決:將 引用 order 處改成 [order]
< class name = "com.Order" table = "[ORDER]" > 4> net.sf.hibernate.exception.SQLGrammarException : Could not save object at net.sf.hibernate.exception.SQLStateConverter.convert( SQLStateConverter.java:58 ) at net.sf.hibernate.exception.JDBCExceptionHelper.convert( JDBCExceptionHelper.java:29 ) at net.sf.hibernate.impl.SessionImpl.convert( SessionImpl.java:4131 ) at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier( SessionImpl.java:794 ) at net.sf.hibernate.impl.SessionImpl.save( SessionImpl.java:749 ) at com.BusinessService.saveCategoryWithCascade( BusinessService.java:54 ) at com.BusinessService.test( BusinessService.java:104 ) at com.BusinessService.main( BusinessService.java:109 ) Caused by: java.sql.SQLException : [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]對象名 'CATEGORIES' 無效。 5〉
錯誤顯示: net.sf.hibernate.MappingException : Resource: Add valid path not found
部分原文件: hibernate.hbm.xml
< hibernate-configuration > < session-factory > < property name = "connection.username" > sa </ property > < property name = "connection.url" > jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test </ property > < property name = "dialect" > net.sf.hibernate.dialect.SQLServerDialect </ property > < property name = "myeclipse.connection.profile" > MSSQL </ property > < property name = "connection.password" > hheryh </ property > < property name = "connection.driver_class" > com.microsoft.jdbc.sqlserver.SQLServerDriver </ property > < mapping resource = "Add valid path" /> </ session-factory >
緣由分析:找不到有效的 xml 文件
問題解決:將全部配置文件都加到 resource 裏
將< mapping resource = "Add valid path" />改成 < mapping resource = "com/Customer.hbm.xml" /> < mapping resource = "com/Order.hbm.xml" /> 6〉 錯誤顯示 net.sf.hibernate.MappingException : Error reading resource: com/Customer.hbm.xml at net.sf.hibernate.cfg.Configuration.addResource( Configuration.java:340 ) at net.sf.hibernate.cfg.Configuration.doConfigure( Configuration.java:1027 ) at net.sf.hibernate.cfg.Configuration.doConfigure( Configuration.java:983 ) at net.sf.hibernate.cfg.Configuration.configure( Configuration.java:911 ) at net.sf.hibernate.cfg.Configuration.configure( Configuration.java:897 ) at com.BusinessService.<clinit>( BusinessService.java:17 ) Caused by: net.sf.hibernate.MappingException : duplicate import: Customer at net.sf.hibernate.cfg.Mappings.addImport( Mappings.java:85 ) at net.sf.hibernate.cfg.Binder.bindClass( Binder.java:126 ) at net.sf.hibernate.cfg.Binder.bindRootClass( Binder.java:221 ) at net.sf.hibernate.cfg.Binder.bindRoot( Binder.java:1256 ) at net.sf.hibernate.cfg.Configuration.add( Configuration.java:253 ) at net.sf.hibernate.cfg.Configuration.addInputStream( Configuration.java:289 ) at net.sf.hibernate.cfg.Configuration.addResource( Configuration.java:337 ) ... 5 more 部分原文件 :hibernate.hbm.xml <? xml version = '1.0' encoding = 'UTF-8' ?> <! DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd" > <!-- Generated by MyEclipse Hibernate Tools. --> < hibernate-configuration > < session-factory > < property name = "connection.username" > sa </ property > < property name = "connection.url" > jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test </ property > < property name = "dialect" > net.sf.hibernate.dialect.SQLServerDialect </ property > < property name = "myeclipse.connection.profile" > MSSQL </ property > < property name = "connection.password" > hheryh </ property > < property name = "connection.driver_class" > com.microsoft.jdbc.sqlserver.SQLServerDriver </ property > < mapping resource = "com/Customer.hbm.xml" /> </ session-factory > </ hibernate-configuration >
主程序:
static{
try{
// Create a configuration based on the properties file we've put
Configuration config = new Configuration();
config.addClass(Customer.class);
// Get the session factory we can use for persistence
sessionFactory = config
.configure()
.buildSessionFactory();
}catch(Exception e){e.printStackTrace();}
}
解決方法: config.addClass(Customer.class);
sessionFactory = config.configure().buildSessionFactory();
緣由分析: hibernaet 配置文件有兩種格式,一種是 xml 格式,一種是普通的 .property 格式 .
在 1.2 版本中,編譯時自動會在 path 路徑中查找 property 格式的配置文件。但不會查詢 xml 格式的配置文件,所以須要在程序中手動添加 config.configure() ,但此時就不要加載了。
上面的程序 加載了一次 config.configure() ,又映射了一次,因此出錯。
解決方法:
若配置文件爲 xml 格式的,程序編寫以下:
// Create a configuration based on the properties file we've put
Configuration config = new Configuration();
// Get the session factory we can use for persistence
sessionFactory = config
.configure()
.buildSessionFactory();
若配置文件爲 property 格式的,程序編寫以下:
// Create a configuration based on the properties file we've put
Configuration config = new Configuration();
config.addClass(Customer.class);
// Get the session factory we can use for persistence
sessionFactory = config.buildSessionFactory();