Hibernete Study Notes

1、Hibernate和各个数据库之间数据类型的映射关系,hbm 文件中映射配置错误,可能出现如下错误(以DB2为例):
SQLCODE=-301, SQLSTATE=07006, SQLERRMC=9

《Java数据类型,Hibernate数据类型,标准sql数据类型之间的对应表》见:
[url]
http://blog.csdn.net/sunyycxy/archive/2006/08/21/1101633.aspx[/url]

2、hibernate3中出现 could not initialize proxy - no Session 错误的解决办法以及lazy load的介绍

错误:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)

代码:
	 	  /** 按照OID查找一个Customer对象 */
	  public Customer findCustomer(Long customer_id){
	    Session session = sessionFactory.openSession();
	    Transaction tx = null;
	    Customer c = null;
	   try{
		   tx = session.beginTransaction();
		   //c = (Customer)session.load(Customer.class, customer_id);
		   c = (Customer)session.get(Customer.class, customer_id);
		   tx.commit();
	   } catch(HibernateException hbe) {
		   if(tx != null)
			   tx.rollback();
		   throw hbe;
	   } finally {
		   session.close(); //当使用session.load()方法加载对象时,如果该行代码执行,则报错:could not initialize proxy - no Session;get()不存在这样的问题。
	   }
	   return c;
	  }

现象:见代码中的注释。
解释:
hibernate 的Session.class API中有如下描述:
    load():This method might return a proxied instance that is initialized on-demand, when a non-identifier method is accessed.
    get():This method never returns an uninitialized instance
    可见load()支持延迟加载,而get()不支持。

参考:
http://fireinwind.iteye.com/blog/753578

你可能感兴趣的:(java,sql,Hibernate,.net,db2)