2013-3-23 0:48:45org.hibernate.internal.SessionFactoryRegistry addSessionFactory
WARN: HHH000277: Could not bindfactory to JNDI
org.hibernate.service.jndi.JndiException: Error parsing JNDI name [mysql]
atorg.hibernate.service.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:92)
atorg.hibernate.service.jndi.internal.JndiServiceImpl.bind(JndiServiceImpl.java:108)
atorg.hibernate.internal.SessionFactoryRegistry.addSessionFactory(SessionFactoryRegistry.java:89)
atorg.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:478)
atorg.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1742)
atorg.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1780)
atme.tspace.test.HibernateTest.main(HibernateTest.java:16)
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or systemproperty, or as an applet parameter, or in an application resource file: java.naming.factory.initial
atjavax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
atjavax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
atjavax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
atjavax.naming.InitialContext.getNameParser(InitialContext.java:480)
atorg.hibernate.service.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:86)
... 6more
hibernate配置文件:
<session-factory name="***" > 错误的
<session-factory >正确
---------------------------------------------------------------------------------------------------------------------------------------
org.hibernate.AnnotationException: @OneToOne or @ManyToOne on me.tspace.hibernate.Students.stuNum references an unknown entity: java.lang.String
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:109)
主表对象应该保存外键对象的引用,加映射关系的会去找对应的实体,然后在外键对象的get方法上加annatation
public class Students {
private int sid;
private String sname;
private StuCard stuCard;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="stuNum",unique=true)
public StuCard getStuCard() {
return stuCard;
}
public void setStuCard(StuCard stuCard) {
this.stuCard = stuCard;
}
}
-------------------------------------------------------------------------------------------------------------------
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [beans.xml]; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor
...........................
Caused by: java.lang.ClassNotFoundException: org.aopalliance.intercept.MethodInterceptor
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 62 more
缺包:aopalliance.jar
下载地址:http://sourceforge.net/projects/aopalliance/files/
------------------------------------------------------------------------------------------------------------------------------
java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
.........
Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 38 more
缺少 aspectjweaver.jar包 地址:
http://mvnrepository.com/artifact/aspectj/aspectjweaver/1.5.3
----------------------------------------------------------------------------------------------------------------------------------
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in class path resource [beans.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider;
.......
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in class path resource [beans.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider;
hibernate4已经将hibernate3的一些功能改掉了,在hibernate4已经不使用CacheProvider了,所以做了以下修改:
原先:<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
改成:<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
新的SessionFactory和TransactionManager的配置:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean>
-----------------------------------------------------------------------------------------------------------------------------------
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in class path resource [beans.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [beans.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined in class path resource [beans.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
说了半天就是说缺少类:org.apache.commons.dbcp.BasicDataSource
缺少:commons-dbcp-1.4.jap
下载地址:http://commons.apache.org/proper/commons-dbcp/download_dbcp.cgi
---------------------------------------------------------------------------------------------------------------------------------------
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in class path resource [beans.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [beans.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool/KeyedObjectPoolFactory
缺少:commons-pool-1.6
下载地址:http://commons.apache.org/proper/commons-pool/download_pool.cgi
---------------------------------------------------------------------------------------------------------------------------------------
java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;
at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:323)
at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:235)
at org.springframework.orm.hibernate3.HibernateTemplate.getSession(HibernateTemplate.java:457)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:392)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:912)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:904)
at me.tspace.personfinance.dao.impl.UserDaoImpl.checkExistByName(UserDaoImpl.java:31)
at me.tspace.personfinance.service.impl.UserServiceImpl.userExists(UserServiceImpl.java:29)
hibernate的HibernateTemplate再hibarenate4.0+Spring3.0中已经不再推荐,直接用SessionFactory,和getcurrentSession,改写daoimpl
参考链接:http://godo121.iteye.com/blog/1473563 http://forum.springsource.org/showthread.php?117227-Missing-Hibernate-Classes-Interfaces-in-spring-orm-3.1.0.RC1
---------------------------------------------------------------------------------------------------------------------------------------------
org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:978)
at me.tspace.personfinance.dao.impl.UserDaoImpl.checkExistByName(UserDaoImpl.java:30)
at me.tspace.personfinance.service.impl.UserServiceImpl.userExists(UserServiceImpl.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
exception is org.hibernate.HibernateException: No Session found for current thread
配置文件方面没配置事物属性:userExists
原因:发现一些bean无法获得当前session,需要把之前一些方法的事务从NOT_SUPPORT提升到required,readonly=true
见https://jira.springsource.org/browse/SPR-9020
http://www.iteye.com/topic/1120924
参考链接:http://godo121.iteye.com/blog/1473563