Hibernate三个小技巧

1.提高hibernate运行速度
2.事务管理错误
3.log错误

Hibernate三个小巧门:

1.提高hibernate运行速度:
将hibernate.cfg.xml文件中的

" 

<!DOCTYPE hibernate-configuration 

 PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" 

 "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> 

"


改成你本地指向你本地的hibernate-configuration-2.0.dtd文件所在位置。例如我的就是:

" 

<!DOCTYPE hibernate-configuration 

 PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" 

 "file:///C:/hibernate-2.1/src/net/sf/hibernate/hibernate-configuration-2.0.dtd"> 

"


即可。

2.事务管理错误:
当行运时出现类似于
"javax.naming.NoInitialContextException: Need to specify class name in environment or

system property, or as an applet parameter, or in an application resource file:

java.naming.factory.initial"

"net.sf.hibernate.TransactionException: Could not find UserTransaction in JNDI:"
时,这是事务管理机制没设置正确。我们一般均采用JDBCTransaction,但hibernate.cfg.xml中可能
是配置成了JTATransaction。只要将:

"<property 


name="hibernate.transaction.factory_class">net.sf.hibernate.transaction.JTATransactionFacto 


ry</property>"


改成:

"<property 


name="hibernate.transaction.factory_class">net.sf.hibernate.transaction.JDBCTransactionFact 


ory</property>"


即可。

3.log错误:
系统若提示出错信息为
"log4j:WARN No appenders could be found for logger (net.sf.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly."
说明系统找不到log4j.properties文件,只要将此文件copy到classes目录下即可解决;另外若您是通过
"Configuration cfg = new Configuration().configure();"这种方式来获取配置的话,那也您应当

将hibernate.cfg.xml文件放置在classes目录下。